Synchronized()与wait()用法

浅显解释Synchronized()与wait()用法

 

 

 

     一直不理解Synchronized()与wait()到底什么意思,到底怎么用。现在有了一些基本的认识,拿来共享

 

这里以一个例子说明问题,注释部分会解释清楚

 

JAVA代码

 

package changhanming;

public class ThreadA {
    public static void main(String[] args) throws InterruptedException {
        ThreadB b = new ThreadB();
        // 启动计算线程,启动B里的run();
        b.start();
      
       synchronized (b) {      //为了防止B里的synchronized同时执行。其实A给B上锁
         
            // 当前线程A等待,这里注意的是wait()方法是使当前线程,A线程等待。
            //若没有wait,则A和B两个线程同步,这时A首先抢占到资源,运行。B还没运行,所以此时计算结果为0
            //若启动wait,则让当前线程A等待。B运行结束后,再唤醒A

         
            b.wait();                  //A放弃B锁,让B执行
          
          
            System.out.println("计算结果:" + b.sum);
        }
      
 class ThreadB extends Thread {
    int total;

    public void run() {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                sum += i;
            }       
            // (完成计算了)唤醒线程A
           notifyAll();
       }
    }
}

 

 

发布了17 篇原创文章 · 获赞 1 · 访问量 1万+
展开阅读全文

java synchronized(this)模块中this参数的含义。

12-08

定义了如下3个类,大致情况的主进程同时启动两个test2和test3线程。 test2和test3的run方法内部都添加了synchronized模块,当两个模块中的参数互指或互不指,即test2种模块参数指向test3同时test3的参数指向test2,或test2和test3的参数都指向自己this时,运行结果为2个test2,test3线程互相等待但test2和test3线程同时运行: new test() t2 0 t3 0 t3 1 t2 1 t3 2 t2 2 t2 3 t3 3 t3 4 t2 4 t3 5 t2 5 t2 0 t3 6 t2 1 t3 7 t3 8 t2 2 t2 3 t3 9 t2 4 t3 10 t2 5 t3 0 t3 1 t3 2 t3 3 t3 4 t3 5 t3 6 t3 7 t3 8 t3 9 t3 10 当test2的参数指向test3,test3的参数指向自己this;或者test2的参数指自己,test3的参数指test2时,运行结果是4个test2,test3线程互相等待,同时只有一个线程运行: new test() t2 0 t2 1 t2 2 t2 3 t2 4 t2 5 t3 0 t3 1 t3 2 t3 3 t3 4 t3 5 t3 6 t3 7 t3 8 t3 9 t3 10 t2 0 t2 1 t2 2 t2 3 t2 4 t2 5 t3 0 t3 1 t3 2 t3 3 t3 4 t3 5 t3 6 t3 7 t3 8 t3 9 t3 10 并且不论synchronized块的参数是什么,同一个对象的运行是被锁住了,即两个test2和两个test3之间只能同时运行一个。 想问下高手jvm如何产生这个特性的原理是什么,这个参数又有什么方面的应用啊? public class test implements Runnable{ Thread t; Thread t1; test2 t2=new test2() ; test3 t3=new test3(); Thread T3; Thread TT3; test() { System.out.println("new test()"); t=new Thread(this); t1=new Thread(this); t2.t3=t3; t3.t2=t2; t.start(); t1.start(); T3=new Thread(t3); TT3=new Thread(t3); T3.start(); TT3.start(); } public void run() { t2.speak(); } public static void main(String[] args) { // TODO Auto-generated method stub new test( ); } } public class test2 { test3 t3; public void speak() { synchronized(t3){ //test2中参数指向test3线程 for(int i=0;i<=5;i++) { System.out.println("t2 "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } public class test3 implements Runnable{ test2 t2; public void run() { synchronized(this){ //synchronized参数指向自己 for(int i=0;i<=10;i++) { System.out.println("t3 "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } 问答

java线程synchronized(this){}为何对其它synchronized方法产生影响。

11-24

直接上代码: public class NewThread implements Runnable{ String name; Thread t; static boolean suspendFlag; NewThread(String threadname) { name=threadname; t=new Thread(this,name); System.out.println("New thread:"+t); suspendFlag=false; t.start(); } public void run() { try { for(int i=15;i>0;i--) { System.out.println(name+":"+i); Thread.sleep(1000); synchronized(this){while(suspendFlag){this.wait();}} } }catch(InterruptedException e){} System.out.println(name+" exiting."); } synchronized void mysuspend(){suspendFlag=true;} synchronized void myresume(){suspendFlag=false;notify();} } public class SuspendResume { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub NewThread ob1=new NewThread("One"); try{ Thread.sleep(2000); ob1.mysuspend(); System.out.println("Suspending thread One"); Thread.sleep(2000); ob1.myresume(); System.out.println("Resuming thread One"); Thread.sleep(2000); }catch(InterruptedException e){} try{System.out.println("Waiting for threads to finiish."); ob1.t.join(); }catch(InterruptedException e){} System.out.println("Main thread exiting."); } } 运行结果: New thread:Thread[One,5,main] One:15 One:14 One:13 Suspending thread One One:12 Resuming thread One One:11 One:10 Waiting for threads to finiish. One:9 One:8 One:7 One:6 One:5 One:4 One:3 One:2 One:1 One exiting. Main thread exiting. 如预期一样,ob1.mysuspend();ob1.myresume();的调用在ob1线程运行中是正常时间控制挂起和重启。 但将run()方法中synchronized(this){}模块稍微修改: public void run() { try { synchronized(this){ for(int i=15;i>0;i--) { System.out.println(name+":"+i); Thread.sleep(1000); while(suspendFlag){this.wait();} } } }catch(InterruptedException e){} System.out.println(name+" exiting."); } 其余代码不变 结果是: New thread:Thread[One,5,main] One:15 One:14 One:13 One:12 One:11 One:10 One:9 One:8 One:7 One:6 One:5 One:4 One:3 One:2 One:1 Suspending thread One One exiting. Resuming thread One Waiting for threads to finiish. Main thread exiting. 结果是主线程对ob1.mysuspend();ob1.myresume();的调用没有按照预期的时间进行,而是卡在ob1线程运行结束后才开始调用,就好像ob1得运行将ob1.mysuspend();ob1.myresume();锁住一样,sychonized模块将循环体包围住,mysuspend()就像被锁住不能动了似的,为什么会有两次的运行结果差异?谢谢。 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览