问题描述:启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15.接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75。
方式一:synchronized结合wait/notifyAll实现
public class ThreeThread {
public static void main(String[] args) {
Object o = new Object();
new Thread(new PrintRunnable(o, 1)).start();
new Thread(new PrintRunnable(o, 2)).start();
new Thread(new PrintRunnable(o, 3)).start();
}
}
class PrintRunnable implements Runnable {
private Object o;
private int threadId;
private static int num = 1;
public PrintRunnable(Object o, int threadId) {
this.o = o;
this.threadId = threadId;
}
@Override
public void run() {
while (num < 65) {//65是因为刚开始3个线程拿到num都为1
synchronized (o) {
while (num / 5 % 3 + 1 != threadId) {
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("线程:" + threadId + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
o.notifyAll();
}
}
}
}
运行结果:
线程:1: 1 2 3 4 5
线程:2: 6 7 8 9 10
线程:3: 11 12 13 14 15
线程:1: 16 17 18 19 20
线程:2: 21 22 23 24 25
线程:3: 26 27 28 29 30
线程:1: 31 32 33 34 35
线程:2: 36 37 38 39 40
线程:3: 41 42 43 44 45
线程:1: 46 47 48 49 50
线程:2: 51 52 53 54 55
线程:3: 56 57 58 59 60
线程:1: 61 62 63 64 65
线程:2: 66 67 68 69 70
线程:3: 71 72 73 74 75
方式二:Condition接口方式实现
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreeThreadWithCondition {
static Lock lock = new ReentrantLock();
static Condition A = lock.newCondition();
static Condition B = lock.newCondition();
static Condition C = lock.newCondition();
static int num = 1;
static int count = 0;
static class PrintRunnableA implements Runnable {
@Override
public void run() {
try {
lock.lock();
while (num < 65) {
while (count % 3 != 0) {
A.await();
}
System.out.print("线程:" + Thread.currentThread().getName() + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
count++;
B.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
static class PrintRunnableB implements Runnable {
@Override
public void run() {
try {
lock.lock();
while (num < 65) {
while (count % 3 != 1) {
B.await();
}
System.out.print("线程:" + Thread.currentThread().getName() + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
count++;
C.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
static class PrintRunnableC implements Runnable {
@Override
public void run() {
try {
lock.lock();
while (num < 65) {
while (count % 3 != 2) {
C.await();
}
System.out.print("线程:" + Thread.currentThread().getName() + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
count = 0;
A.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
new Thread(new PrintRunnableA()).start();
new Thread(new PrintRunnableB()).start();
new Thread(new PrintRunnableC()).start();
}
}
运行结果:
线程:Thread-0: 1 2 3 4 5
线程:Thread-1: 6 7 8 9 10
线程:Thread-2: 11 12 13 14 15
线程:Thread-0: 16 17 18 19 20
线程:Thread-1: 21 22 23 24 25
线程:Thread-2: 26 27 28 29 30
线程:Thread-0: 31 32 33 34 35
线程:Thread-1: 36 37 38 39 40
线程:Thread-2: 41 42 43 44 45
线程:Thread-0: 46 47 48 49 50
线程:Thread-1: 51 52 53 54 55
线程:Thread-2: 56 57 58 59 60
线程:Thread-0: 61 62 63 64 65
线程:Thread-1: 66 67 68 69 70
线程:Thread-2: 71 72 73 74 75
方式三:LockSupport方式实现
import java.util.concurrent.locks.LockSupport;
public class ThreeThreadLockSupport {
static int num = 1;
static int count = 0;
Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();
public void doPrint() {
t1 = new Thread(new Runnable() {
@Override
public void run() {
while (num < 65) {
while (count % 3 != 0) {
LockSupport.park();
}
System.out.print("线程:" + Thread.currentThread().getName() + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
count++;
LockSupport.unpark(t2);
}
}
}, "t1");
t1.start();
t2 = new Thread(new Runnable() {
@Override
public void run() {
while (num < 65) {
while (count % 3 != 1) {
LockSupport.park();
}
System.out.print("线程:" + Thread.currentThread().getName() + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
count++;
LockSupport.unpark(t3);
}
}
}, "t2");
t2.start();
t3 = new Thread(new Runnable() {
@Override
public void run() {
while (num < 65) {
while (count % 3 != 2) {
LockSupport.park();
}
System.out.print("线程:" + Thread.currentThread().getName() + ": ");
for (int i = 5; i > 0; i--, num++) {
System.out.print(" " + num);
}
System.out.println();
count = 0;
LockSupport.unpark(t1);
}
}
}, "t3");
t3.start();
}
public static void main(String[] args) {
ThreeThreadLockSupport lockSupport = new ThreeThreadLockSupport();
lockSupport.doPrint();
}
}
运行结果:
线程:t1: 1 2 3 4 5
线程:t2: 6 7 8 9 10
线程:t3: 11 12 13 14 15
线程:t1: 16 17 18 19 20
线程:t2: 21 22 23 24 25
线程:t3: 26 27 28 29 30
线程:t1: 31 32 33 34 35
线程:t2: 36 37 38 39 40
线程:t3: 41 42 43 44 45
线程:t1: 46 47 48 49 50
线程:t2: 51 52 53 54 55
线程:t3: 56 57 58 59 60
线程:t1: 61 62 63 64 65
线程:t2: 66 67 68 69 70
线程:t3: 71 72 73 74 75
今天突然接收到基友抛出的问题,花了点时间总结了下实现方法。
学无止境,每天进步一点点 ~