三条线程A线程打印我 B线程打印爱 C线程打印你 如此循环十次(2种方法)

public class Abc {
    int flag=1;//开关 为1时A执行,2时B执行,3时C执行

    public synchronized void Aa(){
        if(flag==1){
        	System.out.print("我");
        	flag=2;
        	this.notify();
        }else{
        	try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }      
    }

    public synchronized void Bb(){
        if(flag==2){
        	System.out.print("爱");
        	flag=3;
        	this.notify();
        }else{
        	try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        
        }
    }

        public synchronized void Cc(){
            if(flag==3){
            	System.out.print("你");
              	flag=1;
            	this.notify();
            }else{
            	try {
    				this.wait();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
            }
        }
}

public class Test {

	public static void main(String[] args) {
		   final Abc fs=new Abc();

           new Thread(new Runnable() {

               public void run() {
            	   for(int i=0;i<10;i++){
                    fs.Aa();
                    fs.Bb();
                    fs.Cc();
                    System.out.println();
            	   }
            	  
               }
           }).start();


	}

}

方法二:


public class DoSome implements Runnable{

    //三个变量 三条线程之间切换执行 一把锁是不够的  2把锁把锁  对象有锁的定义 Object对象

    private String word;//线程要打印的字
    private Object prev;//当前线程的上一个线程要持有的锁
    private Object current;//当前线程所要持有的锁

    public void run() {
        for(int i=0;i<10;i++){
              synchronized (prev){
                  synchronized (current){
                      System.out.print(word);
                      current.notify();
                  }
                  try {
                      prev.wait();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
        }
    }

    public DoSome(String word, Object prev, Object current) {
        this.word = word;
        this.prev = prev;
        this.current = current;
    }
    //打印字的操作
}
public class Test {
    //
    public static void main(String[] args) throws InterruptedException {

        Object a =new Object();
        Object b=new Object();
        Object c=new Object();

        Thread t1=new Thread(new DoSome("我",c,a));
        Thread t2=new Thread(new DoSome("爱",a,b));
        Thread t3=new Thread(new DoSome("你",b,c));


        t1.start();
        Thread.sleep(1000);
        t2.start();
        Thread.sleep(1000);
        t3.start();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值