forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSynLock.java
More file actions
66 lines (56 loc) · 1.52 KB
/
SynLock.java
File metadata and controls
66 lines (56 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package JavaBasic;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @Classname SynLock
* @Description 使用Lock对象来实现同步机制
* @Date 19-5-25 下午4:04
* @Created by mao<tianmao818@qq.com>
*/
class Callme_1{
private Lock lock=new ReentrantLock();
void call(String msg){
lock.lock();
try{
System.out.print("lock: ["+msg);
try{
Thread.sleep(1000);
}catch (InterruptedException e){
System.out.println("Interrupted");
}
System.out.println("]");
}finally {
lock.unlock();
}
}
}
class Caller_1 implements Runnable{
String msg;
Callme_1 target;
Thread t;
public Caller_1(Callme_1 tar,String s){
target=tar;
msg=s;
t=new Thread(this);
t.start();
}
public void run(){
target.call(msg);
}
}
public class SynLock {
public static void main(String[] args){
Callme_1 target=new Callme_1();
//没有组织三个线程同时调用同一个对象的同一个方法,在call前面加上synchronized关键字
Caller_1 ob1=new Caller_1(target,"hello");
Caller_1 ob2=new Caller_1(target,"synchronized");
Caller_1 ob3=new Caller_1(target,"world");
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch (InterruptedException e){
System.out.println("interrupted");
}
}
}