java-notify/wait写给自己看的 三个线程abc顺序打印

1.notify 必须放在 synchronized(obj){}代码块中
synchronized(lock){
    if(){
        lock.notifyAll();
    }else{
        lock.wait();
    }

}

2.如果没有写在synchronized(obj){}中时直接lock.notifyAll();会报错

3.必须在一个线程类中写逻辑,不能在多个线程类中写
   TempThread a = new TempThread();
   TempThread b = new TempThread();
   TempThread c = new TempThread();
   a.start();
   b.start();
   c.start();  //这种是可以的
   
   TempThreadA a = new TempThreadA();
   TempThreadB b = new TempThreadB();
   TempThreadC c = new TempThreadC();
   a.start();
   b.start();
   c.start();  //这种是不行的
----------三个线程abc顺序打印10次
我写的代码

package com.qbsea.knowledge.morethread;

public class AbcTest2 {
	static class SynchLock {
		private volatile String active = "A";
		private static int count = 0;
		public static void add() {
			synchronized (SynchLock.class) {
				count = count + 1;
			}
		}
	}
	public static void main(String[] args) throws InterruptedException {
		SynchLock lock = new SynchLock();
		PrintA a = new PrintA(lock,"A","B");
		PrintA b = new PrintA(lock,"B","C");
		PrintA c = new PrintA(lock,"C","A");
		a.start();
		b.start();
		c.start();
	}

	static class PrintA extends Thread {
		private SynchLock lock;
		private String name;
		private String next;

		PrintA(SynchLock lock,String name,String next) {
			this.lock = lock;
			this.name=name;
			this.next=next;
		}

		@Override
		public void run() {
			while(true) {
				synchronized (lock) {
					if (this.name.equals(lock.active)) {
						if("A".equals(this.name)&&lock.count>=3) {
							break;
						}
					
						System.out.println(this.getName() + " "+this.name);
						lock.active=this.next;
						if("A".equals(this.name)) {
							lock.add();
						}
						lock.notifyAll();
					} else {
						try {
							lock.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			
		}
	}

}

网上有人写的优化的代码
 

package com.qbsea.knowledge.morethread;

import java.util.concurrent.atomic.AtomicInteger;

public class AbcTest {

	public static void main(String argv[]) {
		AtomicInteger synObj = new AtomicInteger(0);
		TestPrint a = new TestPrint(synObj, "A", 0);
		TestPrint b = new TestPrint(synObj, "B", 1);
		TestPrint c = new TestPrint(synObj, "C", 2);
		a.start();
//		b.start();
//		c.start();
	}

	static class TestPrint extends Thread {
		private AtomicInteger synObj;
		private String name;
		private int flag;
		private int count = 0;
		public TestPrint(AtomicInteger synObj, String name, int flag) {
			this.synObj = synObj;
			this.name = name;
			this.flag = flag;
		}
		@Override
		public void run() {
			while (true) {
				synchronized (synObj) {//锁同一个对象
					if (synObj.get() % 3 == flag) {//等 于自己
						synObj.set(synObj.get() + 1);
						System.out.println(name);
						count++;
						synObj.notifyAll();
						if (count == 10) {
							break;
						}
					} else {
						try {
							synObj.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/maqingbin8888/article/details/81239468