java多线程等待,唤醒,以及线程同步,使用synchronized关键字实现。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mengtianqq/article/details/80830259

java多线程等待,唤醒,以及线程同步。

 在Object.java中,定义了wait(), notify()和notifyAll()等接口。wait()的作用是让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁。而notify()和notifyAll()的作用,则是唤醒当前对象上的等待线程;notify()是唤醒单个线程,而notifyAll()是唤醒所有的线程。结合同步锁synchronized一起使用可以做到java多线程等待,唤醒,使用 synchronized 关键字修饰,并使用“{}”括起来的代码片段,它表示同一时间只能有一个线程进入到该方法块中,是一种多线程保护机制。等。

创建Student类:

public class Student {
	String name;
    int age;
     int  count;
    boolean flag = false; // 表示没有值
}

SetThread类:

public class SetThread implements Runnable {
		// TODO Auto-generated method stub
		 private Student s;
		    private int x = 0;

		    public SetThread(Student s) {
		        this.s = s;
		    }

		    public void run() {
		        while (true) {
		            synchronized (s) {
		                // 判断,如果有值,就等待
		                if (s.flag) {
		                    try {
		                        s.wait(); //t1就等待在这里了。
		                    } catch (InterruptedException e) {
		                        e.printStackTrace();
		                    }
		                }

		                // 设置值
		                if (x % 2 == 0) {
		                    s.name = "天才";
		                    s.age = 15;
		                } else {
		                    s.name = "废材";
		                    s.age = 30;
		                }
		                x++; //x=1,x=2,

		                // 有值后,就修改标记
		                s.flag = true;
		                s.notify(); // 唤醒等待的线程,唤醒其他的线程,不代表其他的线程能够立即执行。
		            }
		            //可能t1抢到,也可能t2抢到
		        }
		    }
}

GetThread类:

public class GetThread  implements Runnable{
	  private Student s;

	    public GetThread(Student s) {
	        this.s = s;
	    }

	    public void run() {
	        while (true) {
	            synchronized (s) {
	                // 判断没有值,就等待
	                if (!s.flag) {
	                    try {
	                        s.wait(); //t2在这等待了。
	                                  //t2线程在这里等待,那么,它就会释放锁对象。
	                                  //将来,当它再次获取到执行权的时候,是从哪里等待,哪里醒来。
	                    } catch (InterruptedException e) {
	                        e.printStackTrace();
	                    }
	                }

	                System.out.println(s.name + "---" + s.age+"  count:"+s.count);
	                s.count +=1;
	                //林青霞---27

	                // 修改标记
	                s.flag = false;
	                s.notify(); //唤醒其他的线程
	            }
	            //可能t2,可能t1
	        }
	    }
	}

测试类:testMain

public class testMain {
  
	public  static void main(String[] args ){
		Student s = new Student();
		s.count =0;
        SetThread st = new SetThread(s);
        GetThread gt = new GetThread(s);
        Thread t1 = new Thread(st);
        Thread t2 = new Thread(gt);
        t1.start();
        t2.start();
        
			}
	
}

结果:

废材---30  count:1232487
天才---15  count:1232488
废材---30  count:1232489
天才---15  count:1232490
废材---30  count:1232491
天才---15  count:1232492
废材---30  count:1232493
天才---15  count:1232494
废材---30  count:1232495
天才---15  count:1232496
废材---30  count:1232497

结果分析:加synchronized同步锁,结合对象student的wait()和notify()方法就可以做到线程执行的流程。

猜你喜欢

转载自blog.csdn.net/mengtianqq/article/details/80830259