一, 创建两个线程,其中一个输出1-52,另外一个输出A-Z。输出格式要求:12A 34B 56C 78D
1.synchronized实现
Test6.java
public class Test6 {
/*
创建两个线程,其中一个输出1-52,另外一个输出A-Z。输出格式要求:12A 34B 56C 78D
*/
public static void main(String[] args) throws InterruptedException {
Object lockA = new Object();
new Thread(new Work1(lockA)).start();
Thread.sleep(100);
new Thread(new WorkA(lockA)).start();
Thread.sleep(10000);
}
}
Work1.java 打印1-52
class Work1 implements Runnable {
/**
* print 1-52
*/
private int count = 0;
private Object lockA;
public Work1(Object lockA) {
this.lockA = lockA;
}
@Override
public void run() {
synchronized (lockA) {
while (count <= 50) {
System.out.print(++count);
System.out.print(++count);
try {
lockA.notify();
lockA.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lockA.notify();
}
}
}
WorkA.java 打印A-Z
class WorkA implements Runnable {
/**
* print A-Z
*/
private char count = 'A';
private Object lockA;
public WorkA(Object lockA) {
this.lockA = lockA;
}
@Override
public void run() {
synchronized (lockA) {
while (count <= 'Z') {
System.out.print(count++);
System.out.print(" ");
try {
lockA.notify();
lockA.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lockA.notify();
}
}
}
运行结果
12A 34B 56C 78D 910E 1112F 1314G 1516H 1718I 1920J 2122K 2324L 2526M 2728N 2930O 3132P 3334Q 3536R 3738S 3940T 4142U 4344V 4546W 4748X 4950Y 5152Z
Process finished with exit code 0
image.png
2.Lock实现
Test7.java
public class Test7 {
public static void main(String[] args) throws InterruptedException {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(new Work17(lock, condition)).start();
Thread.sleep(100);
new Thread(new WorkA7(lock, condition)).start();
Thread.sleep(3000);
}
}
Work17.java 打印1-52
class Work17 implements Runnable {
/**
* print 1-52
*/
private int count = 0;
private Lock lock;
private Condition condition;
public Work17(Lock lock, Condition condition) {
this.lock = lock;
this.condition = condition;
}
@Override
public void run() {
try {
lock.tryLock();
while (count <= 50) {
System.out.print(++count);
System.out.print(++count);
condition.signal();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
condition.signalAll();
} finally {
lock.unlock();
}
}
}
WorkA7.java 打印A-Z
class WorkA7 implements Runnable {
/**
* print A-Z
*/
private char count = 'A';
private Lock lock;
private Condition condition;
public WorkA7(Lock lock, Condition condition) {
this.lock = lock;
this.condition = condition;
}
@Override
public void run() {
try {
lock.tryLock();
while (count <= 'Z') {
System.out.print(count++);
System.out.print(" ");
condition.signal();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
condition.signalAll();
} finally {
lock.unlock();
}
}
}
运行结果
image.png
二, 编写程序实现,子线程循环3次,接着主线程循环5次,接着再子线程循环3次,主线程循环5次,如此反复,循环3次.
Test3.java
public class Test3 {
/*
编写程序实现,子线程循环3次,接着主线程循环5次,接着再子线程循环3次,主线程循环5次,如此反复,循环3次.
*/
public static void main(String[] args) throws InterruptedException {
Object lockA = new Object();
Object lockB = new Object();
MyThread myThread1 = new MyThread(5, lockA, lockB, "main-Thread");
MyThread myThread2 = new MyThread(3, lockB, lockA, "sub-Thread");
myThread2.start();
Thread.sleep(200);
myThread1.start();
Thread.sleep(3000);
}
static class MyThread extends Thread {
private int count;
private Object lockA;
private Object lockB;
public MyThread(Integer count, Object lockA, Object lockB, String name) {
super(name);
this.count = count;
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
int icount = 0;
while (icount < 3) {
synchronized (lockB){
synchronized (lockA) {
for (int i = 0; i < count; i++) {
System.out.println("线程名: " + Thread.currentThread().getName() + " 次数: " + i);
}
lockA.notify();
}
try {
if (icount < 2) {
lockB.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
icount++;
}
}
}
}
运行结果:
线程名: sub-Thread 次数: 0
线程名: sub-Thread 次数: 1
线程名: sub-Thread 次数: 2
线程名: main-Thread 次数: 0
线程名: main-Thread 次数: 1
线程名: main-Thread 次数: 2
线程名: main-Thread 次数: 3
线程名: main-Thread 次数: 4
线程名: sub-Thread 次数: 0
线程名: sub-Thread 次数: 1
线程名: sub-Thread 次数: 2
线程名: main-Thread 次数: 0
线程名: main-Thread 次数: 1
线程名: main-Thread 次数: 2
线程名: main-Thread 次数: 3
线程名: main-Thread 次数: 4
线程名: sub-Thread 次数: 0
线程名: sub-Thread 次数: 1
线程名: sub-Thread 次数: 2
线程名: main-Thread 次数: 0
线程名: main-Thread 次数: 1
线程名: main-Thread 次数: 2
线程名: main-Thread 次数: 3
线程名: main-Thread 次数: 4
Process finished with exit code 0
三, 创建三个线程打印 A B C, 要求打印顺序为ABCABCABC
1.synchronized实现
Test1.java
public class Test1 {
public static void main(String[] args) throws InterruptedException {
Printer a = new Printer("A");
Printer b = new Printer("B");
Printer c = new Printer("C");
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (c) {
synchronized (a) {
a.work();
a.notify();
}
c.wait();
}
}
} catch (Exception e) {
}
}
}).start();
//sleep 保证首次顺序
Thread.sleep(100);
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (a) {
synchronized (b) {
b.work();
b.notify();
}
a.wait();
}
}
} catch (Exception e) {
}
}
}).start();
//sleep 保证首次顺序
Thread.sleep(100);
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (b) {
synchronized (c) {
c.work();
c.notify();
}
b.wait();
}
}
} catch (Exception e) {
}
}
}).start();
Thread.sleep(10000);
}
}
Worker.java 线程
class Worker implements Runnable {
private Printer printer;
public Worker(Printer printer) {
this.printer = printer;
}
@Override
public void run() {
synchronized (printer) {
printer.work();
}
}
}
Printer.java 打印类
class Printer {
private String out;
public Printer(String out) {
this.out = out;
}
public void work() {
try {
//sleep 避免刷屏太快
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(out);
}
}
运行结果:
ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC...
Process finished with exit code -1
网友评论