Multithreading: lock synchronized code block, synchronized method, a method using static synchronized

 

In the process of learning in multi-threaded, locking a lot of information we will point out is this, a static synchronized method lock synchronized code blocks and synchronized using a method like the lock, then starting from this conclusion, how to reverse prove it?

Proof of this lock

``

public class ThreadDemo3 {
   public static void main(String[] args) throws InterruptedException{
       MyThread3 mt = new MyThread3();
       Thread t1 = new Thread(mt, "窗口1");
       Thread t2 = new Thread(mt, "窗口2");
       t1.start();
       Thread.sleep(40);
       mt.flag = false;
       t2.start();
  }
}

class MyThread3 implements Runnable {
   int tickets = 100;
   boolean flag = true;
   Object obj = new Object();

   public void run() {
       if (flag) {
           while (tickets > 0) {
               synchronized (obj) {
                   if (tickets > 0) {
                       try {
                           Thread.sleep(40);
                      } catch (InterruptedException e) {
                           e.printStackTrace();
                      }
                       System.out.println(Thread.currentThread().getName() + "售出了" + (100 - tickets + 1) + "张票");
                       tickets--;
                  }
              }
          }
      } else {
           while (tickets > 0) {
               sell();
          }
      }
  }

   synchronized public void sell() {
       if (tickets > 0) {
           try {
               Thread.sleep(40);
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
           System.out.println(Thread.currentThread().getName() + "售出了" + (100 - tickets + 1) +"张票");
           tickets--;
      }
  }
}

 

Code, as briefly described the idea, the synchronized with the sync block as a lock obj time, the result of printing: printing a random two threads, tickets sold from a first 101, a thread-safe problem described occurs, thereby to obtain conclusions t1 and t2 the thread is not executed in synchronism;

Then the synchronization code block lock into this object, executed again, the result of printing: printing a random two threads, tickets sold from the first 100.

 

Proof Locks

The above code again slightly modified, will sell methods, flag the object, tickets plus static modifier objects, run the code again, you can see the result has printed 101 tickets, this description is not locked static methods used, according to our Conclusion, the synchronized synchronized block in the lock replaced by "MyThread3.class" and then run the program, synchronization phenomenon!

 

In this way we can verify the conclusions of the beginning

 

Make a small summary, in the process of writing this code, make a problem, the while loop wrote synchronized block of code, this will lead to future thread synchronization into single-threaded execution, the thread t2 will never get executed Opportunity! ! Therefore, we must pay attention during the multi-thread coding, the synchronization code block range

 

Guess you like

Origin www.cnblogs.com/blogforvi/p/12164225.html