两线程交替打印1-100(synchronized 交替等待-唤醒)
import java.util.concurrent.TimeUnit;
public class TwoThreadPrintSync {
static int num = 1;
static int END = 100;
public static void main(String[] args) throws InterruptedException {
new Thread(TwoThreadPrintSync::compute, "t1").start();
new Thread(TwoThreadPrintSync::compute, "t2").start();
TimeUnit.MICROSECONDS.sleep(1);
synchronized (TwoThreadPrintSync.class) {
TwoThreadPrintSync.class.notify();
}
}
public static void compute(){
synchronized (TwoThreadPrintSync.class) {
while (true) {
try {
TwoThreadPrintSync.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (num>END) break;
System.out.println(Thread.currentThread().getName() + "-" + num++);
TwoThreadPrintSync.class.notify();
}
TwoThreadPrintSync.class.notify();
}
}
}
两线程交替打印1-100(ReentrantLock 变量控制)
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class TwoThreadPrintNum {
static int num = 1;
static boolean odd = true;
static int END = 100;
static ReentrantLock lock = new ReentrantLock();
static Condition condition = lock.newCondition();
public static void main(String[] args) {
new Thread(() -> {
lock.lock();
try {
while (num <= END) {
if (odd) {
System.out.println(Thread.currentThread().getName() + "#####" + num++);
odd = false;
condition.signal();
} else {
condition.await();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}, "t1").start();
new Thread(() -> {
lock.lock();
try {
while (num <= END) {
if (!odd) {
System.out.println(Thread.currentThread().getName() + "-----" + num++);
odd = true;
condition.signal();
} else {
condition.await();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}, "t2").start();
}
}
三线程交替打印1-100(synchronized 变量控制)
package com.example.concurrenttest;
public class SequecenPrint {
public static void main(String[] args) {
PrintNum printNum = new PrintNum(1, 1, 100);
new Thread(() -> printNum.print(1, 2), "t1").start();
new Thread(() -> printNum.print(2, 3), "t2").start();
new Thread(() -> printNum.print(3, 1), "t3").start();
}
}
class PrintNum {
private int current;
private int num;
private final int END;
public PrintNum(int current, int begin, int end) {
num = begin;
this.current = current;
this.END = end;
}
public void print(int flag, int next) {
synchronized (this) {
while (true) {
while (current != flag && num <= END) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (num > END) break;
System.out.println(Thread.currentThread().getName() + "-" + num++);
this.current = next;
this.notifyAll();
}
}
}
}
三线程交替打印1-100(ReentrantLock 交替唤醒)
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ThreeThreadPrintNum {
static int num = 1;
static int END = 100;
static ReentrantLock lock = new ReentrantLock();
static Condition condition1 = lock.newCondition();
static Condition condition2 = lock.newCondition();
static Condition condition3 = lock.newCondition();
public static void print(Condition current, Condition next) {
lock.lock();
try {
while (true) {
current.await();
if (num > END) break;
System.out.println(Thread.currentThread().getName() + "-" + num++);
next.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
next.signal();
lock.unlock();
}
}
public static void main(String[] args) {
new Thread(() -> print(condition1, condition2), "t1").start();
new Thread(() -> print(condition2, condition3), "t2").start();
new Thread(() -> print(condition3, condition1), "t3").start();
if (lock.tryLock()) {
try {
condition1.signal();
} finally {
lock.unlock();
}
}
}
}
三线程交替打印1-100(park-unpark)
import java.util.concurrent.locks.LockSupport;
public class ParkUnpark {
static final int END = 100;
static int num = 1;
static Thread t1;
static Thread t2;
static Thread t3;
public static void main(String[] args) {
t1 = new Thread(() -> ParkUnpark.print(t2), "t1");
t2 = new Thread(() -> ParkUnpark.print(t3), "t2");
t3 = new Thread(() -> ParkUnpark.print(t1), "t3");
t1.start();
t2.start();
t3.start();
LockSupport.unpark(t1);
}
public static void print(Thread next) {
while (true) {
LockSupport.park();
if (num > END) break;
System.out.println(Thread.currentThread().getName() + "-" + num++);
LockSupport.unpark(next);
}
LockSupport.unpark(next);
}
}