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

面试题

多线程轮流顺序打印 a,b,c三个线程按顺序打印数字1-96 线程a打印1,b打印2,c打印3 ......  具体的程序实现见如下连接

1.notify 必须放在 synchronized(obj){}代码块中   while(true) 挂一个循环体
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();
						}
					}
				}
			}
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值