synchronized(this)

  此时,线程获得的是对象锁.例如: 
 1 public class Thread8 extends Thread {
 2     
 3     public void sync_fun() {
 4         synchronized (this) {
 5             int i = 5;
 6             while (i-- > 0) {
 7                 System.out
 8                         .println(Thread.currentThread().getName() + " : " + i);
 9                 try {
10                     Thread.sleep(500);
11                 } catch (InterruptedException ie) {
12                 }
13             }
14         }
15     }
16     public static void main(String args[]) {
17         final Thread8 t8 = new Thread8();
18         Thread t1 = new Thread(new Runnable() {
19             @Override
20             public void run() {
21                 t8.sync_fun();
22             }
23         }, "t1");
24         Thread t2 = new Thread(new Runnable() {
25             @Override
26             public void run() {
27                 t8.sync_fun();
28             }
29         }, "t2");
30         Thread t3 = new Thread(new Runnable() {
31             @Override
32             public void run() {
33                 t8.sync_fun();
34             }
35         }, "t3");
36         Thread t4 = new Thread(new Runnable() {
37             @Override
38             public void run() {
39                 t8.sync_fun();
40             }
41         }, "t4");
42         Thread t5 = new Thread(new Runnable() {
43             @Override
44             public void run() {
45                 t8.sync_fun();
46             }
47         }, "t4");
48         Thread t6 = new Thread(new Runnable() {
49             @Override
50             public void run() {
51                 t8.sync_fun();
52             }
53         }, "t4");
54         t1.start();
55         t2.start();
56         t3.start();
57         t4.start();
58         t5.start();
59         t6.start();
60     }
61 }

结果:

t1 : 4
t1 : 3
t1 : 2
t1 : 1
t1 : 0
t4 : 4
t4 : 3
t4 : 2
t4 : 1
t4 : 0
t4 : 4
t4 : 3
t4 : 2
t4 : 1
t4 : 0
t4 : 4
t4 : 3
t4 : 2
t4 : 1
t4 : 0
t3 : 4
t3 : 3
t3 : 2
t3 : 1
t3 : 0
t2 : 4
t2 : 3
t2 : 2
t2 : 1
t2 : 0

 

     当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。在对象级使用锁通常是一种比较粗糙的方法。为什么要将整个对象都上锁,而不允许其他线程短暂地使用对象中其他同步方法来访问共享资源?如果一个对象拥有多个资源,就不需要只为了让一个线程使用其中一部分资源,就将所有线程都锁在外面。由于每个对象都有锁,可以如下所示使用虚拟对象来上锁:

 1 class FineGrainLock {
 2 
 3    MyMemberClass x, y;
 4    Object xlock = new Object(), ylock = new Object();
 5 
 6    public void foo() {
 7       synchronized(xlock) {
 8          //access x here
 9       }
10 
11       //do something here - but don't use shared resources
12 
13       synchronized(ylock) {
14          //access y here
15       }
16    }
17 
18    public void bar() {
19       synchronized(this) {
20          //access both x and y here
21       }
22       //do something here - but don't use shared resources
23    }
24 }

 

相关文章:

  • 2022-12-23
  • 2021-06-25
  • 2021-07-07
  • 2018-05-19
  • 2021-06-16
猜你喜欢
  • 2022-03-11
  • 2022-02-26
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-01-14
相关资源
相似解决方案