一.基本方法
Synchronized用于线程同步与互斥,用法有以下两种:
synchronized 方法和 synchronized 代码块
方法1.synchronized 方法:通过在方法声明中加入 synchronized 关键字来声明 synchronized 方法
public synchronized void methodName(class className);
方法2.synchronized 代码块:通过 synchronized关键字来声明 synchronized 代码块
synchronized(this) {//允许访问控制的代码
}
二.实现线程互斥
示例代码1 (使用 synchronized 方法):
1 public classTest {
2
3 public static voidmain(String[] args) {
4 newTest().init();
5 }
6
7 private voidinit(){
8 Outputer outputer = newOutputer();
9 //创建第一个线程
10 new Thread(newRunnable(){
11 public voidrun() {
12 while(true){
13 try{
14 Thread.sleep(10);
15 } catch(InterruptedException e) {
16 e.printStackTrace();
17 }
18 outputer.output("shen_smile");
19 }
20 }
21 }).start();
22 //创建第二个线程
23 new Thread(newRunnable(){
24 public voidrun() {
25 while(true){
26 try{
27 Thread.sleep(10);
28 } catch(InterruptedException e) {
29 e.printStackTrace();
30 }
31 outputer.output("SynchronizedTest");
32 }
33 }
34 }).start();
35 }
36
37 classOutputer{
38 //用synchronized方法实现互斥调用
39 public synchronized voidoutput(String name){
40 int len =name.length();
41 for(int i=0;i
42 System.out.print(name.charAt(i));//将name以字母为单位打印
43 }
44 System.out.println();
45 }
46 }
47 }
示例代码2 (使用 synchronized 代码块):
1 public classTest {2
3 public static voidmain(String[] args) {4 newTest().init();5 }6
7 private voidinit(){8 Outputer outputer = newOutputer();9 //创建第一个线程
10 new Thread(newRunnable(){11 public voidrun() {12 while(true){13 try{14 Thread.sleep(10);15 } catch(InterruptedException e) {16 e.printStackTrace();17 }18 outputer.output("shen_smile");19 }20 }21 }).start();22 //创建第二个线程
23 new Thread(newRunnable(){24 public voidrun() {25 while(true){26 try{27 Thread.sleep(10);28 } catch(InterruptedException e) {29 e.printStackTrace();30 }31 outputer.output("SynchronizedTest");32 }33 }34 }).start();35 }36 classOutputer{
37 //用synchronized代码块实现互斥调用38 public voidoutput(String name){39 int len =name.length();40 synchronized(this){41 for(int i=0;i
43 }44 System.out.println();45 }46 }47 }48 }
三.实现线程同步
结合 synchronized方法, synchronized代码块 和 静态synchronized方法:
1.实现 synchronized方法和 synchronized代码块同步
这里实现的是两个线程对相同对象的不同方法之间的同步,两个方法分别用 synchronized方法和 synchronized代码块实现,本例中 output方法中的synchronized应该写成方法2的标准样式:
synchronized(this) {
//允许访问控制的代码
}
示例代码:
1 public classTest {2
3 public static voidmain(String[] args) {4 newTest().init();5 }6
7 private voidinit(){8 Outputer outputer = newOutputer();9 //创建第一个线程
10 new Thread(newRunnable(){11 public voidrun() {12 while(true){13 try{14 Thread.sleep(10);15 } catch(InterruptedException e) {16 e.printStackTrace();17 }18 outputer.output("shen_smile");19 }20 }21 }).start();22 //创建第二个线程
23 new Thread(newRunnable(){24 public voidrun() {25 while(true){26 try{27 Thread.sleep(10);28 } catch(InterruptedException e) {29 e.printStackTrace();30 }31 outputer.output2("SynchronizedTest");32 }33 }34 }).start();35 }36
37 classOutputer{38 //synchronized代码块
39 public voidoutput(String name){40 int len =name.length();41 synchronized(this){42 for(int i=0;i
44 }45 System.out.println();46 }47 }48 //synchronized方法
49 public synchronized voidoutput2(String name){50 int len =name.length();51 for(int i=0;i
57 }
2.实现 静态synchronized方法和 synchronized代码块同步
这里实现的是两个线程对相同对象的 静态synchronized方法和 synchronized代码块同步,普通synchronized代码块方法要和静态synchronized方法同步,就必须在synchronized代码块方法中使用类的字节码,本例中字节码为 Outputer.class
即,将以下代码
synchronized(this) {
//允许访问控制的代码
}
改为
synchronized(Outputer.class) {
//允许访问控制的代码
}
示例代码:
1 public classTest {2
3 public static voidmain(String[] args) {4 newTest().init();5 }6
7 private voidinit(){8 Outputer outputer = newOutputer();9 //创建第一个线程
10 new Thread(newRunnable(){11 public voidrun() {12 while(true){13 try{14 Thread.sleep(10);15 } catch(InterruptedException e) {16 e.printStackTrace();17 }18 outputer.output("shen_smile");19 }20 }21 }).start();22 //创建第二个线程
23 new Thread(newRunnable(){24 public voidrun() {25 while(true){26 try{27 Thread.sleep(10);28 } catch(InterruptedException e) {29 e.printStackTrace();30 }31 outputer.output3("SynchronizedTest");32 }33 }34 }).start();35 }36
37 static classOutputer{38 //synchronized代码块
39 public voidoutput(String name){40 int len =name.length();41 synchronized(Outputer.class){42 for(int i=0;i
44 }45 System.out.println();46 }47 }48 //静态synchronized方法
49 public static synchronized voidoutput3(String name){50 int len =name.length();51 for(int i=0;i