线程8锁

Posted wylwyl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程8锁相关的知识,希望对你有一定的参考价值。

题目:打印是one还是two

第一张情况:两个普通同步方法,打印结果是

One
Two

 

 1 package day1;
 2 
 3 public class TestThread8Monitor {
 4 
 5     public static void main(String[] args) {
 6         Number number = new Number();
 7         
 8         new Thread(new Runnable() {
 9             
10             @Override
11             public void run() {
12                 number.getOne();
13                 
14             }
15         }).start();
16         
17         new Thread(new Runnable() {
18             
19             @Override
20             public void run() {
21                 number.getTwo();
22             }
23         }).start();
24     }
25 
26 }
27 
28 class Number {
29     
30     public synchronized void getOne() {
31         System.out.println("One");
32     }
33     
34     public synchronized void getTwo() {
35         System.out.println("Two");
36     }
37 }

 

第二种情况:在getOne方法中新增一个Thread.sleep()调用,打印结果

One
Two

 1 package day1;
 2 
 3 public class TestThread8Monitor {
 4 
 5     public static void main(String[] args) {
 6         Number number = new Number();
 7         
 8         new Thread(new Runnable() {
 9             
10             @Override
11             public void run() {
12                 number.getOne();
13                 
14             }
15         }).start();
16         
17         new Thread(new Runnable() {
18             
19             @Override
20             public void run() {
21                 number.getTwo();
22             }
23         }).start();
24     }
25 
26 }
27 
28 class Number {
29     
30     public synchronized void getOne() {
31         
32         try {
33             Thread.sleep(3000);
34         } catch (Exception e) {
35             // TODO: handle exception
36         }
37         
38         System.out.println("One");
39     }
40     
41     public synchronized void getTwo() {
42         System.out.println("Two");
43     }
44 }

 

第三种情况:在Number类中新增一个普通方法,打印结果

Three
One
Two

/**
 * 
 */
package day1;

public class TestThread8Monitor {

    public static void main(String[] args) {
        Number number = new Number();
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                number.getOne();
                
            }
        }).start();
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                number.getTwo();
            }
        }).start();
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                number.getThree();
            }
        }).start();
    }

}

class Number {
    
    public synchronized void getOne() {
        
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            // TODO: handle exception
        }
        
        System.out.println("One");
    }
    
    public synchronized void getTwo() {
        System.out.println("Two");
    }
    
    public void getThree() {
        System.out.println("Three");
    }
}

 

第四种情况:两个普通同步方法,两个Number对象,打印结果

Two
One

 1 /**
 2  * 
 3  */
 4 package day1;
 5 
 6 public class TestThread8Monitor {
 7 
 8     public static void main(String[] args) {
 9         Number number = new Number();
10         Number number2 = new Number();
11         
12         new Thread(new Runnable() {
13             
14             @Override
15             public void run() {
16                 number.getOne();
17                 
18             }
19         }).start();
20         
21         new Thread(new Runnable() {
22             
23             @Override
24             public void run() {
25                 number2.getTwo();
26             }
27         }).start();
28         
29         
30     }
31 
32 }
33 
34 class Number {
35     
36     public synchronized void getOne() {
37         
38         try {
39             Thread.sleep(3000);
40         } catch (Exception e) {
41             // TODO: handle exception
42         }
43         
44         System.out.println("One");
45     }
46     
47     public synchronized void getTwo() {
48         System.out.println("Two");
49     }
50     
51     public void getThree() {
52         System.out.println("Three");
53     }
54 }

 

第五种情况:修改getOne()为静态同步方法,打印结果

Two
One

 

 1 /**
 2  * 
 3  */
 4 package day1;
 5 
 6 public class TestThread8Monitor {
 7 
 8     public static void main(String[] args) {
 9         Number number = new Number();
10         
11         new Thread(new Runnable() {
12             
13             @Override
14             public void run() {
15                 number.getOne();
16                 
17             }
18         }).start();
19         
20         new Thread(new Runnable() {
21             
22             @Override
23             public void run() {
24                 number.getTwo();
25             }
26         }).start();
27         
28         
29     }
30 
31 }
32 
33 class Number {
34     
35     public static synchronized void getOne() {
36         
37         try {
38             Thread.sleep(3000);
39         } catch (Exception e) {
40             // TODO: handle exception
41         }
42         
43         System.out.println("One");
44     }
45     
46     public synchronized void getTwo() {
47         System.out.println("Two");
48     }
49     
50     public void getThree() {
51         System.out.println("Three");
52     }
53 }

 

第六种情况:修改两个方法均为静态同步方法,一个Number对象,打印结果

One
Two

 

 1 /**
 2  * 
 3  */
 4 package day1;
 5 
 6 public class TestThread8Monitor {
 7 
 8     public static void main(String[] args) {
 9         Number number = new Number();
10         
11         new Thread(new Runnable() {
12             
13             @Override
14             public void run() {
15                 number.getOne();
16                 
17             }
18         }).start();
19         
20         new Thread(new Runnable() {
21             
22             @Override
23             public void run() {
24                 number.getTwo();
25             }
26         }).start();
27         
28         
29     }
30 
31 }
32 
33 class Number {
34     
35     public static synchronized void getOne() {
36         
37         try {
38             Thread.sleep(3000);
39         } catch (Exception e) {
40             // TODO: handle exception
41         }
42         
43         System.out.println("One");
44     }
45     
46     public static synchronized void getTwo() {
47         System.out.println("Two");
48     }
49     
50     public void getThree() {
51         System.out.println("Three");
52     }
53 }

 

第七章情况:一种是静态同步方法,一个非静态同步方法,两个Number对象,打印结果

Two
One

 1 /**
 2  * 
 3  */
 4 package day1;
 5 
 6 public class TestThread8Monitor {
 7 
 8     public static void main(String[] args) {
 9         Number number = new Number();
10         Number number2 = new Number();
11         
12         new Thread(new Runnable() {
13             
14             @Override
15             public void run() {
16                 number.getOne();
17                 
18             }
19         }).start();
20         
21         new Thread(new Runnable() {
22             
23             @Override
24             public void run() {
25                 number2.getTwo();
26             }
27         }).start();
28         
29         
30     }
31 
32 }
33 
34 class Number {
35     
36     public static synchronized void getOne() {
37         
38         try {
39             Thread.sleep(3000);
40         } catch (Exception e) {
41             // TODO: handle exception
42         }
43         
44         System.out.println("One");
45     }
46     
47     public synchronized void getTwo() {
48         System.out.println("Two");
49     }
50     
51     public void getThree() {
52         System.out.println("Three");
53     }
54 }

 

第第八种情况:两个静态同步方法,两个Number对象,打印

One
Two

 

 1 /**
 2  * 
 3  */
 4 package day1;
 5 
 6 public class TestThread8Monitor {
 7 
 8     public static void main(String[] args) {
 9         Number number = new Number();
10         Number number2 = new Number();
11         
12         new Thread(new Runnable() {
13             
14             @Override
15             public void run() {
16                 number.getOne();
17                 
18             }
19         }).start();
20         
21         new Thread(new Runnable() {
22             
23             @Override
24             public void run() {
25                 number2.getTwo();
26             }
27         }).start();
28         
29         
30     }
31 
32 }
33 
34 class Number {
35     
36     public static synchronized void getOne() {
37         
38         try {
39             Thread.sleep(3000);
40         } catch (Exception e) {
41             // TODO: handle exception
42         }
43         
44         System.out.println("One");
45     }
46     
47     public static synchronized void getTwo() {
48         System.out.println("Two");
49     }
50     
51     public void getThree() {
52         System.out.println("Three");
53     }
54 }

 

以上是关于线程8锁的主要内容,如果未能解决你的问题,请参考以下文章

并发技术12线程锁技术的使用

java并发线程锁技术的使用

互斥锁 & 共享锁

线程方法区别

起底多线程同步锁(iOS)

java 多线程8 : synchronized锁机制 之 方法锁