forked from fivemoons/DataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncThread.java
More file actions
34 lines (33 loc) · 804 Bytes
/
Copy pathSyncThread.java
File metadata and controls
34 lines (33 loc) · 804 Bytes
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
package sync;
public class SyncThread extends Thread{
private static String sync = "";
private String methodType = "";
private static void method(String s){
synchronized(sync){
sync = s;
System.out.println(s);
while(true);
}
}
public void method1(){
method("method1");
}
public static void staticMethod1(){
method("staticMethod1");
}
public void run(){
if(methodType.equals("static"))
staticMethod1();
else if (methodType.equals("nonstatic"))
method1();
}
public SyncThread(String methodType){
this.methodType = methodType;
}
public static void main(String[] args) throws Exception{
SyncThread sample1 = new SyncThread("nonstatic");
SyncThread sample2 = new SyncThread("static");
sample1.start();
sample2.start();
}
}