wait()方法及notify()练习题

1.写两个线程,一个线程打印 1~52,另一个线程打印A~Z,
打印顺序是12A34B...5152Z;

class printNumber implements Runnable{
    private Object obj;//声明一个类的引用
    public printNumber(Object obj){
        this. obj= obj;//通过构造器将共享的资源-->对象传进来
    }
    @Override
    public void run() {
       synchronized (obj){//给共享资源上锁
           for(int i=1;i<53;i++){
               System.out.println(i);
               if(i%2==0){
                   obj.notifyAll();//唤醒其他线程
                   try{
                       obj.wait();//等待并释放锁
                   }catch(InterruptedException e){
                       e.printStackTrace();
                   }
               }
           }
        }
    }
}
class printLetter implements Runnable{
    private Object obj;
    public printLetter(Object obj){
        this. obj= obj;
    }
    @Override
    public void run() {
        synchronized (obj){
            for(int i=0;i<26;i++){
                System.out.println((char)(i+'A'));
                obj.notifyAll();
                try {
                    obj.wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
public class test{
    public static void main(String[] args) {
        Object object=new Object();
        printNumber number=new printNumber(object);
        printLetter letter =new printLetter(object);
        Thread thread1=new Thread(number);
        Thread thread2=new Thread(letter);
        thread1.start();
        thread2.start();
    }
}
  • 创建两个线程实现Runnable()接口覆写run()方法,一个打印字母,一个打印数字。其中数字每打印两个就唤醒其他线程,释放锁,进入阻塞状态。字母每打印一次唤醒其他线程,释放锁,进入阻塞状态。
  • 在两个类的run方法中都要用synchronized保证同步,即加锁
  • 创建一个测试类,在测试类中创建一个Object类的对象(作为两个线程的共享资源,以便实现线程间的通信),通过各类的构造方法传递过去。
  • 两个线程要使用同一个资源才能相互通信,所以在测试类中创建共享资源,并通过构造方法分别传到各线程类中。
  • 两个线程哪个先运行(执行start())哪个就先获得资源并执行
  • 在run方法体内写进程间的通信wait()和notifyall()时,一定要先写notifyall()再写wait()
  • 当你先写wait()时,本进程也进入休眠状态,再写notifyall()唤醒所有线程时本线程以及其他线程被一块唤醒,竞争同一个资源,就会造成死锁。

2.编写一个程序,启动三个线程,三个线程的名称分别是 A,B,C;
每个线程将自己的名称在屏幕上打印5遍,打印顺序是ABCABC...

class ThreadA implements Runnable{
    private Printer printer;
    public ThreadA(Printer printer){
        this.printer=printer;
    }
    @Override
    public void run() {
        for(int i=0;i<5;i++){
            printer.printThreadA();
        }
    }
}
class ThreadB implements Runnable{
    private Printer printer;
    public ThreadB(Printer printer){
        this.printer=printer;
    }
    @Override
    public void run() {
        for(int i=0;i<5;i++){
            printer.printThreadB();
        }
    }
}
class ThreadC implements Runnable{
    private Printer printer;
    public ThreadC(Printer printer){
        this.printer=printer;
    }
    @Override
    public void run() {
        for(int i=0;i<5;i++){
            printer.printThreadC();
        }
    }
}
class Printer{
    int flag=0;
    public synchronized  void printThreadA(){
        if(flag==0){
            System.out.println("A");
            flag=1;
            notifyAll();
        }
        try{
            wait();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    } public synchronized  void printThreadB(){
        if(flag==1){
            System.out.println("B");
            flag=2;
            notifyAll();
        }
        try{
            wait();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
    public synchronized  void printThreadC(){
        if(flag==2){
            System.out.println("C");
            flag=0;
            notifyAll();
        }
        try{
            wait();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}
public class test{
   public static void main(String[] args) {
        Printer printer=new Printer();
        ThreadA threadA=new ThreadA(printer);
       ThreadB threadB=new ThreadB(printer);
       ThreadC threadC=new ThreadC(printer);
       Thread thread1=new Thread(threadA);
       thread1.start();
       Thread thread2=new Thread(threadB);
       thread2.start();
       Thread thread3=new Thread(threadC);
       thread3.start();
   }
}
  • 创建三个线程实现Runnable()接口覆写run()方法,三个线程的名字分别为A,B,C 
  • 创建pointer类来实现三个线程的打印操作,为5次。

3.写3个线程,打印如下
*Thread-0
*Thread-0@Thread-1
*Thread-0@Thread-1#Thread-2
*Thread-0@Thread-1#Thread-2*Thread-0
*Thread-0@Thread-1#Thread-2*Thread-0@Thread-1
*Thread-0@Thread-1#Thread-2*Thread-0@Thread-1#Thread-2
...
重复30次

class MyThread {
    private Object obj=new Object();	//循环次数
    private int count;	//打印次数
    private int flag=0;
    public MyThread(int count) {
        this.count=count;
        }
        public void fun() {
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<count;i++) {
                    synchronized(obj) {
                        if(flag%3==0 &&
                              ("Thread-0").equals(Thread.currentThread().getName())) {
                            System.out.println("*Thread-0");
                          flag++;
                            obj.notifyAll();
                           }else {
                           try {
                               obj.wait();
                            }
                           catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
       });
        Thread thread2=new Thread(new Runnable() {		@Override
        public void run() {
            for(int i=0;i<count;i++) {
                synchronized (obj) {
                    if(flag%3==1&&("Thread-1".equals(Thread.currentThread().getName()))) {
                        System.out.println("#Thread-1");
                       flag++;
                        obj.notifyAll();
                       try {
                           obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                      }
                   }
                }
            }
       }
        });
        Thread thread3=new Thread(new Runnable() {
            @Override
            public void run() {

               for(int i=0;i<count;i++) {
                   synchronized (obj) {
                       if(flag%3==2&&("Thread-2".equals(Thread.currentThread().getName()))) {
                           System.out.println("@Thread-2");
                           flag++;
                           obj.notifyAll();
                        }else {
                           try {
                               obj.wait();
                            } catch (InterruptedException e) {
                               e.printStackTrace();
                          }
                       }
                   }
               }
           }
       });
        thread1.start();
       thread2.start();
       thread3.start();
   }
}
public class test {
    public static void main(String[] args) {
        MyThread myThread=new MyThread(30);
       myThread.fun();
   }
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值