3つのスレッドが順番にABCを印刷し、ABCABCABCの順に......(3つの方式)
15748 ワード
1.synchronized悲観ロックの使用
(秋招阿里的笔记试题,应该写的比较复杂,然后没有后o(ㄛㄛ)o)
2.Lock+Conditionの使用
3.Semaphoreによる実装
また、リンク3つのスレッドを参照して順番印刷ABC(一):Semaphoreを用いて信号量Semaphoreを用いてABCを循環印刷ABCを実現3つのスレッドを用いて順番印刷ABCを交互に実行ABC(二):Lock+Conditionを用いて3つのスレッドを交互に順番印刷ABCを実現(三):Lockを用いて実現
(秋招阿里的笔记试题,应该写的比较复杂,然后没有后o(ㄛㄛ)o)
public class ThreadThreadp {
private int flag = 0;
public synchronized void printa() throws InterruptedException {
while (true)
{
if(flag ==0)
{
System.out.print("A");
flag = 1;
notifyAll();
}
wait();
}
}
public synchronized void printb() throws InterruptedException {
while (true)
{
if(flag ==1)
{
System.out.print("B");
flag = 2;
notifyAll();
}
wait();
}
}
public synchronized void printc() throws InterruptedException {
while (true) {
if (flag == 2) {
System.out.print("C");
Thread.sleep(1000);
flag = 0;
notifyAll();
}
wait();
}
}
public static void main(String[]args) throws InterruptedException
{
ThreadThreadp t = new ThreadThreadp();
PrintA printA = new PrintA(t);
PrintB printB = new PrintB(t);
PrintC printC = new PrintC(t);
Thread t1 = new Thread(printA);
Thread t2 = new Thread(printB);
Thread t3 = new Thread(printC);
t1.start();
t2.start();
t3.start();
//Thread t11 = new Thread(printA);
//Thread t21 = new Thread(printB);
//Thread t31 = new Thread(printC);
//t11.start();
//t21.start();
//t31.start();
}
}
class PrintA implements Runnable{
private ThreadThreadp t;
PrintA(ThreadThreadp t){
this.t=t;
}
@Override
public void run() {
try {
t.printa();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class PrintB implements Runnable{
private ThreadThreadp t;
PrintB(ThreadThreadp t){
this.t=t;
}
@Override
public void run() {
try {
t.printb();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class PrintC implements Runnable{
private ThreadThreadp t;
PrintC(ThreadThreadp t){
this.t=t;
}
@Override
public void run() {
try {
t.printc();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.Lock+Conditionの使用
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class threadsan {
public static void main(String [] args)
{
PrintABC printABC = new PrintABC();
new Thread(new Runnable() {
@Override
public void run() {
printABC.printA();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
printABC.printB();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
printABC.printC();
}
}).start();
}
}
class PrintABC{
private final Lock lock = new ReentrantLock();
private Condition lockA = lock.newCondition();
private Condition lockB = lock.newCondition();
private Condition lockC = lock.newCondition();
int flag = 0;
public void printA()
{
lock.lock();
try {
while (true)
{
while (flag!=0)
lockA.await();
System.out.print("A");
flag =1;
lockB.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printB()
{
lock.lock();
try {
while (true)
{
while (flag!=1)
lockB.await();
System.out.print("B");
flag =2;
lockC.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printC()
{
lock.lock();
try {
while (true)
{
while (flag!=2)
lockC.await();
System.out.print("C");
Thread.sleep(1000);
flag =0;
lockA.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
3.Semaphoreによる実装
//semaphore 。。。
import java.util.concurrent.Semaphore;
/**
* Created by huali on 2018/7/25.
*/
public class PrintABCRotationUsingSemaphore {
public static void main(String[] args) {
PrintABCUsingSemaphore printABC = new PrintABCUsingSemaphore();
new Thread(() -> printABC.printA()).start();
new Thread(() -> printABC.printB()).start();
new Thread(() -> printABC.printC()).start();
}
}
class PrintABCUsingSemaphore {
private Semaphore semaphoreA = new Semaphore(1);
private Semaphore semaphoreB = new Semaphore(0);
private Semaphore semaphoreC = new Semaphore(0);
//private int attempts = 0;
public void printA() {
print("A", semaphoreA, semaphoreB);
}
public void printB() {
print("B", semaphoreB, semaphoreC);
}
public void printC() {
print("C", semaphoreC, semaphoreA);
}
private void print(String name, Semaphore currentSemaphore, Semaphore nextSemaphore) {
for (int i = 0; i < 10; ) {
try {
currentSemaphore.acquire();
//System.out.println(Thread.currentThread().getName()+" try to print "+name+", attempts : "+(++attempts));
System.out.println(Thread.currentThread().getName() +" print "+ name);
i++;
nextSemaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
また、リンク3つのスレッドを参照して順番印刷ABC(一):Semaphoreを用いて信号量Semaphoreを用いてABCを循環印刷ABCを実現3つのスレッドを用いて順番印刷ABCを交互に実行ABC(二):Lock+Conditionを用いて3つのスレッドを交互に順番印刷ABCを実現(三):Lockを用いて実現