synchronized 见解
Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。当两个并发线程访问同一个对象object中的这个加锁同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。然而,当一个线程访问object的一个加锁代码块时,另一个线程仍可以访问该object中的非加锁代码块。
代码例子(测试数据的时候 给的数据越多 效果越明显)
public class TestSync1 implements Runnable{
@Override
public void run() {
show();
synchronized(this){
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName()+"吃了编号为:"+i +"的苹果");
}
}
}
public void show(){
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"非同步:"+i );
}
}
public static void main(String[] args) {
TestSync1 apple=new TestSync1();
Thread t1=new Thread(apple);
Thread t2=new Thread(apple);
Thread t3=new Thread(apple);
t1.start();
t3.start();
t2.start();
}
}
执行结果
Thread-1非同步:0
Thread-1非同步:1
Thread-1非同步:2
Thread-1非同步:3
Thread-1非同步:4
Thread-1吃了编号为:0的苹果
Thread-1吃了编号为:1的苹果
Thread-1吃了编号为:2的苹果
Thread-1吃了编号为:3的苹果
Thread-1吃了编号为:4的苹果
Thread-1吃了编号为:5的苹果
Thread-1吃了编号为:6的苹果
Thread-1吃了编号为:7的苹果
Thread-1吃了编号为:8的苹果
Thread-1吃了编号为:9的苹果
Thread-1吃了编号为:10的苹果
Thread-1吃了编号为:11的苹果
Thread-1吃了编号为:12的苹果
Thread-1吃了编号为:13的苹果
Thread-1吃了编号为:14的苹果
Thread-1吃了编号为:15的苹果
Thread-1吃了编号为:16的苹果
Thread-1吃了编号为:17的苹果
Thread-1吃了编号为:18的苹果
Thread-1吃了编号为:19的苹果
Thread-0非同步:0
Thread-2非同步:0
Thread-2非同步:1
Thread-2非同步:2
Thread-2非同步:3
Thread-2非同步:4
Thread-0非同步:1
Thread-0非同步:2
Thread-0非同步:3
Thread-0非同步:4
Thread-2吃了编号为:0的苹果
Thread-2吃了编号为:1的苹果
Thread-2吃了编号为:2的苹果
Thread-2吃了编号为:3的苹果
Thread-2吃了编号为:4的苹果
Thread-2吃了编号为:5的苹果
Thread-2吃了编号为:6的苹果
Thread-2吃了编号为:7的苹果
Thread-2吃了编号为:8的苹果
Thread-2吃了编号为:9的苹果
Thread-2吃了编号为:10的苹果
Thread-2吃了编号为:11的苹果
Thread-2吃了编号为:12的苹果
Thread-2吃了编号为:13的苹果
Thread-2吃了编号为:14的苹果
Thread-2吃了编号为:15的苹果
Thread-2吃了编号为:16的苹果
Thread-2吃了编号为:17的苹果
Thread-2吃了编号为:18的苹果
Thread-2吃了编号为:19的苹果
Thread-0吃了编号为:0的苹果
Thread-0吃了编号为:1的苹果
Thread-0吃了编号为:2的苹果
Thread-0吃了编号为:3的苹果
Thread-0吃了编号为:4的苹果
Thread-0吃了编号为:5的苹果
Thread-0吃了编号为:6的苹果
Thread-0吃了编号为:7的苹果
Thread-0吃了编号为:8的苹果
Thread-0吃了编号为:9的苹果
Thread-0吃了编号为:10的苹果
Thread-0吃了编号为:11的苹果
Thread-0吃了编号为:12的苹果
Thread-0吃了编号为:13的苹果
Thread-0吃了编号为:14的苹果
Thread-0吃了编号为:15的苹果
Thread-0吃了编号为:16的苹果
Thread-0吃了编号为:17的苹果
Thread-0吃了编号为:18的苹果
Thread-0吃了编号为:19的苹果
不难发现
修饰非静态方法时,线程A访问对象A的非静态同步方法A时,其他线程可以访问该对象的非同步方法以及其他对象的任何方法
如果修饰静态方法呢?
public class TestSync2 implements Runnable{
@Override
public void run() {
show();
add();
}
public static synchronized void show(){
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"静态同步:"+i );
}
}
public synchronized void add(){
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"非静态同步:"+i );
}
}
public static void main(String[] args) {
TestSync2 apple=new TestSync2();
Thread t1=new Thread(apple);
Thread t2=new Thread(apple);
Thread t3=new Thread(apple);
t1.start();
t3.start();
t2.start();
}
}
运行结果
Thread-1静态同步:0
Thread-1静态同步:1
Thread-1静态同步:2
Thread-1静态同步:3
Thread-1静态同步:4
Thread-1静态同步:5
Thread-1静态同步:6
Thread-1静态同步:7
Thread-1静态同步:8
Thread-1静态同步:9
Thread-1非静态同步:0
Thread-1非静态同步:1
Thread-1非静态同步:2
Thread-1非静态同步:3
Thread-1非静态同步:4
Thread-1非静态同步:5
Thread-1非静态同步:6
Thread-1非静态同步:7
Thread-1非静态同步:8
Thread-1非静态同步:9
Thread-1非静态同步:10
Thread-1非静态同步:11
Thread-1非静态同步:12
Thread-1非静态同步:13
Thread-1非静态同步:14
Thread-1非静态同步:15
Thread-1非静态同步:16
Thread-1非静态同步:17
Thread-1非静态同步:18
Thread-1非静态同步:19
Thread-1非静态同步:20
Thread-1非静态同步:21
Thread-1非静态同步:22
Thread-1非静态同步:23
Thread-1非静态同步:24
Thread-1非静态同步:25
Thread-1非静态同步:26
Thread-1非静态同步:27
Thread-1非静态同步:28
Thread-1非静态同步:29
Thread-1非静态同步:30
Thread-1非静态同步:31
Thread-1非静态同步:32
Thread-1非静态同步:33
Thread-1非静态同步:34
Thread-1非静态同步:35
Thread-1非静态同步:36
Thread-1非静态同步:37
Thread-1非静态同步:38
Thread-1非静态同步:39
Thread-1非静态同步:40
Thread-1非静态同步:41
Thread-1非静态同步:42
Thread-1非静态同步:43
Thread-1非静态同步:44
Thread-1非静态同步:45
Thread-1非静态同步:46
Thread-1非静态同步:47
Thread-1非静态同步:48
Thread-1非静态同步:49
Thread-0静态同步:0
Thread-0静态同步:1
Thread-0静态同步:2
Thread-0静态同步:3
Thread-0静态同步:4
Thread-0静态同步:5
Thread-0静态同步:6
Thread-0静态同步:7
Thread-0静态同步:8
Thread-0静态同步:9
Thread-0非静态同步:0
Thread-0非静态同步:1
Thread-0非静态同步:2
Thread-0非静态同步:3
Thread-0非静态同步:4
Thread-0非静态同步:5
Thread-0非静态同步:6
Thread-0非静态同步:7
Thread-0非静态同步:8
Thread-0非静态同步:9
Thread-0非静态同步:10
Thread-0非静态同步:11
Thread-2静态同步:0
Thread-0非静态同步:12
Thread-0非静态同步:13
Thread-0非静态同步:14
Thread-2静态同步:1
Thread-2静态同步:2
Thread-2静态同步:3
Thread-2静态同步:4
Thread-0非静态同步:15
Thread-0非静态同步:16
Thread-2静态同步:5
Thread-0非静态同步:17
Thread-2静态同步:6
Thread-0非静态同步:18
Thread-2静态同步:7
Thread-0非静态同步:19
Thread-0非静态同步:20
Thread-0非静态同步:21
Thread-0非静态同步:22
Thread-0非静态同步:23
Thread-0非静态同步:24
Thread-2静态同步:8
Thread-0非静态同步:25
Thread-0非静态同步:26
Thread-0非静态同步:27
Thread-0非静态同步:28
Thread-0非静态同步:29
Thread-2静态同步:9
Thread-0非静态同步:30
Thread-0非静态同步:31
Thread-0非静态同步:32
Thread-0非静态同步:33
Thread-0非静态同步:34
Thread-0非静态同步:35
Thread-0非静态同步:36
Thread-0非静态同步:37
Thread-0非静态同步:38
Thread-0非静态同步:39
Thread-0非静态同步:40
Thread-0非静态同步:41
Thread-0非静态同步:42
Thread-0非静态同步:43
Thread-0非静态同步:44
Thread-0非静态同步:45
Thread-0非静态同步:46
Thread-0非静态同步:47
Thread-0非静态同步:48
Thread-0非静态同步:49
Thread-2非静态同步:0
Thread-2非静态同步:1
Thread-2非静态同步:2
Thread-2非静态同步:3
Thread-2非静态同步:4
Thread-2非静态同步:5
Thread-2非静态同步:6
Thread-2非静态同步:7
Thread-2非静态同步:8
Thread-2非静态同步:9
Thread-2非静态同步:10
Thread-2非静态同步:11
Thread-2非静态同步:12
Thread-2非静态同步:13
Thread-2非静态同步:14
Thread-2非静态同步:15
Thread-2非静态同步:16
Thread-2非静态同步:17
Thread-2非静态同步:18
Thread-2非静态同步:19
Thread-2非静态同步:20
Thread-2非静态同步:21
Thread-2非静态同步:22
Thread-2非静态同步:23
Thread-2非静态同步:24
Thread-2非静态同步:25
Thread-2非静态同步:26
Thread-2非静态同步:27
Thread-2非静态同步:28
Thread-2非静态同步:29
Thread-2非静态同步:30
Thread-2非静态同步:31
Thread-2非静态同步:32
Thread-2非静态同步:33
Thread-2非静态同步:34
Thread-2非静态同步:35
Thread-2非静态同步:36
Thread-2非静态同步:37
Thread-2非静态同步:38
Thread-2非静态同步:39
Thread-2非静态同步:40
Thread-2非静态同步:41
Thread-2非静态同步:42
Thread-2非静态同步:43
Thread-2非静态同步:44
Thread-2非静态同步:45
Thread-2非静态同步:46
Thread-2非静态同步:47
Thread-2非静态同步:48
Thread-2非静态同步:49
public class TestSync2 implements Runnable{
@Override
public void run() {
show();
add();
}
public static synchronized void show(){
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"静态同步:"+i );
}
}
public static synchronized void add(){
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"静态同步:"+i );
}
}
public static void main(String[] args) {
TestSync2 apple=new TestSync2();
Thread t1=new Thread(apple);
Thread t2=new Thread(apple);
Thread t3=new Thread(apple);
t1.start();
t3.start();
t2.start();
}
}
运行结果
Thread-0静态同步:0
Thread-0静态同步:1
Thread-0静态同步:2
Thread-0静态同步:3
Thread-0静态同步:4
Thread-0静态同步:5
Thread-0静态同步:6
Thread-0静态同步:7
Thread-0静态同步:8
Thread-0静态同步:9
Thread-0静态同步:0
Thread-0静态同步:1
Thread-0静态同步:2
Thread-0静态同步:3
Thread-0静态同步:4
Thread-0静态同步:5
Thread-0静态同步:6
Thread-0静态同步:7
Thread-0静态同步:8
Thread-0静态同步:9
Thread-0静态同步:10
Thread-0静态同步:11
Thread-0静态同步:12
Thread-0静态同步:13
Thread-0静态同步:14
Thread-0静态同步:15
Thread-0静态同步:16
Thread-0静态同步:17
Thread-0静态同步:18
Thread-0静态同步:19
Thread-0静态同步:20
Thread-0静态同步:21
Thread-0静态同步:22
Thread-0静态同步:23
Thread-0静态同步:24
Thread-0静态同步:25
Thread-0静态同步:26
Thread-0静态同步:27
Thread-0静态同步:28
Thread-0静态同步:29
Thread-0静态同步:30
Thread-0静态同步:31
Thread-0静态同步:32
Thread-0静态同步:33
Thread-0静态同步:34
Thread-0静态同步:35
Thread-0静态同步:36
Thread-0静态同步:37
Thread-0静态同步:38
Thread-0静态同步:39
Thread-0静态同步:40
Thread-0静态同步:41
Thread-0静态同步:42
Thread-0静态同步:43
Thread-0静态同步:44
Thread-0静态同步:45
Thread-0静态同步:46
Thread-0静态同步:47
Thread-0静态同步:48
Thread-0静态同步:49
Thread-1静态同步:0
Thread-1静态同步:1
Thread-1静态同步:2
Thread-1静态同步:3
Thread-1静态同步:4
Thread-1静态同步:5
Thread-1静态同步:6
Thread-1静态同步:7
Thread-1静态同步:8
Thread-1静态同步:9
Thread-1静态同步:0
Thread-1静态同步:1
Thread-1静态同步:2
Thread-1静态同步:3
Thread-1静态同步:4
Thread-1静态同步:5
Thread-1静态同步:6
Thread-1静态同步:7
Thread-1静态同步:8
Thread-1静态同步:9
Thread-1静态同步:10
Thread-1静态同步:11
Thread-1静态同步:12
Thread-1静态同步:13
Thread-1静态同步:14
Thread-1静态同步:15
Thread-1静态同步:16
Thread-1静态同步:17
Thread-1静态同步:18
Thread-1静态同步:19
Thread-1静态同步:20
Thread-1静态同步:21
Thread-1静态同步:22
Thread-1静态同步:23
Thread-1静态同步:24
Thread-1静态同步:25
Thread-1静态同步:26
Thread-1静态同步:27
Thread-1静态同步:28
Thread-1静态同步:29
Thread-1静态同步:30
Thread-1静态同步:31
Thread-1静态同步:32
Thread-1静态同步:33
Thread-1静态同步:34
Thread-1静态同步:35
Thread-1静态同步:36
Thread-1静态同步:37
Thread-1静态同步:38
Thread-1静态同步:39
Thread-1静态同步:40
Thread-1静态同步:41
Thread-1静态同步:42
Thread-1静态同步:43
Thread-1静态同步:44
Thread-1静态同步:45
Thread-1静态同步:46
Thread-1静态同步:47
Thread-1静态同步:48
Thread-1静态同步:49
Thread-2静态同步:0
Thread-2静态同步:1
Thread-2静态同步:2
Thread-2静态同步:3
Thread-2静态同步:4
Thread-2静态同步:5
Thread-2静态同步:6
Thread-2静态同步:7
Thread-2静态同步:8
Thread-2静态同步:9
Thread-2静态同步:0
Thread-2静态同步:1
Thread-2静态同步:2
Thread-2静态同步:3
Thread-2静态同步:4
Thread-2静态同步:5
Thread-2静态同步:6
Thread-2静态同步:7
Thread-2静态同步:8
Thread-2静态同步:9
Thread-2静态同步:10
Thread-2静态同步:11
Thread-2静态同步:12
Thread-2静态同步:13
Thread-2静态同步:14
Thread-2静态同步:15
Thread-2静态同步:16
Thread-2静态同步:17
Thread-2静态同步:18
Thread-2静态同步:19
Thread-2静态同步:20
Thread-2静态同步:21
Thread-2静态同步:22
Thread-2静态同步:23
Thread-2静态同步:24
Thread-2静态同步:25
Thread-2静态同步:26
Thread-2静态同步:27
Thread-2静态同步:28
Thread-2静态同步:29
Thread-2静态同步:30
Thread-2静态同步:31
Thread-2静态同步:32
Thread-2静态同步:33
Thread-2静态同步:34
Thread-2静态同步:35
Thread-2静态同步:36
Thread-2静态同步:37
Thread-2静态同步:38
Thread-2静态同步:39
Thread-2静态同步:40
Thread-2静态同步:41
Thread-2静态同步:42
Thread-2静态同步:43
Thread-2静态同步:44
Thread-2静态同步:45
Thread-2静态同步:46
Thread-2静态同步:47
Thread-2静态同步:48
Thread-2静态同步:49
修饰静态方法时,作用于类,同时作用于该类的所有对象。所以,线程A访问静态同步方法时,其他线程可以访问非静态同步方法和非同步方法,不可以访问静态同步方法。
总结:
如果一个方法内带有synchronized (类名.class)这样的代码块,这个方法就相当于静态同步方法。
线程A方法该类的静态同步方法时,其他线程不可以访问静态同步方法,但是可以访问非静态同步方法和非同步方法。
原文链接:https://www.cnblogs.com/lwx521/p/9031752.html