-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadTest1.java
More file actions
54 lines (45 loc) · 1.29 KB
/
ThreadTest1.java
File metadata and controls
54 lines (45 loc) · 1.29 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
package shixunTest;
/*
* synchronize能够锁住的前提条件:线程必须持有同一个对象的锁
*
*
*多个线程执行时无序的,是由jvn决定
*作用是安全操作,但性能降低
*缺陷:锁定的范围太大----------因此,提出用synchronize同步块进行安全操作(锁定范围更小,尽量在降低性能的损耗)
*
* 同步块中的()必须是引用对象,不能使基本参数类型
*
* */
public class ThreadTest1 extends Thread{
//synchronize能够锁住的前提条件:线程必须持有同一个对象的锁
public void run() {
Integer j=0;
synchronized (j) {
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
}
class ThreadTest2 implements Runnable{
@Override
public void run() {
System.out.println("欢迎:"+Thread.currentThread().getName());
synchronized (this) {
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
}
class Test{
public static void main(String[] args) {
ThreadTest1 thread1=new ThreadTest1();
ThreadTest1 thread2=new ThreadTest1();
thread1.start();
thread2.start();
/*ThreadTest2 threadTest2=new ThreadTest2();
new Thread(threadTest2).start();
new Thread(threadTest2).start();*/
}
}