大家好,又见面了,我是全栈君。
/**
* @author admin
* @date 2018/1/12 9:48
* 作用在同一个实例对象上讨论
* synchronized同步方法的测试
* 两个线程,一个线程调用synchronized修饰方法,另一个线程可以调用非synchronized修饰的方法,互不影响
*/
public class SynchronizedTest {
public synchronized void methodA() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodA-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void methodB() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodB-" + i );
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest test = new SynchronizedTest();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
test.methodA();
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
test.methodB();
}
});
thread2.start();
}
}
运行结果:
methodA-0
methodB-0
methodA-1
methodB-1
methodB-2
methodA-2
methodA-3
methodB-3
methodA-4
methodB-4
/**
* @author admin
* @date 2018/1/12 10:16
* 作用在同一个实例对象上讨论
* Sychronized代码块的测试
* 两个线程,一个线程执行synchronized代码块,另一个线程执行非synchronized代码块
*/
public class SychronizedTest2 {
public void methodA() {
synchronized (this) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodA-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void methodB() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodB-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SychronizedTest2 test2 = new SychronizedTest2();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
test2.methodA();
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
test2.methodB();
}
});
thread2.start();
}
}
运行结果:
methodA-0
methodB-0
methodA-1
methodB-1
methodA-2
methodB-2
methodB-3
methodA-3
methodA-4
methodB-4
/**
* @author admin
* @date 2018/1/12 10:33
* 作用在同一个实例对象上讨论
* Synchronized同步方法和同步代码块
* 1、synchronized和synchronized(this)二者没区别,都作用在this对象锁上面,所以会同步
* 2、synchronized(obj),这个是作用在obj对象锁上面,和this对象锁不同,所以不会同步
*/
public class SynchronizedTest3 {
public synchronized void methodA() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodA-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void methodB() {
synchronized (this) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodB-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void methodC() {
Object obj = new Object();
synchronized (obj) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodC-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedTest3 test3 = new SynchronizedTest3();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
test3.methodA();
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
test3.methodB();
}
});
thread2.start();
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
test3.methodC();
}
});
thread3.start();
}
}
运行结果:
methodA-0
methodC-0
methodA-1
methodC-1
methodA-2
methodC-2
methodA-3
methodC-3
methodA-4
methodC-4
methodB-0
methodB-1
methodB-2
methodB-3
methodB-4
/**
* @author admin
* @date 2018/1/12 10:48
* 作用在同一个类上讨论,每一个类只有一个类锁
* synchronized类锁
* static synchronized 和 synchronized(SynchronizedTest4.class),都是作用在同一个类锁上,所以会同步
*/
public class SynchronizedTest4 {
public synchronized static void methodA() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodA-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void methodB() {
synchronized (SynchronizedTest4.class) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodB-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedTest4 test4 = new SynchronizedTest4();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
test4.methodA();
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
test4.methodB();
}
});
thread2.start();
}
}
运行结果:
methodA-0
methodA-1
methodA-2
methodA-3
methodA-4
methodB-0
methodB-1
methodB-2
methodB-3
methodB-4
/**
* @author admin
* @date 2018/1/12 11:03
* synchronized的对象锁和static synchronized的类锁,是两个不同的锁,所以不会同步
* 两个线程,一个调用对象锁,一个调用类锁
*/
public class SynchronizedTest5 {
public synchronized void methodA() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodA-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized static void methodB() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("methodB-" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest5 test5 = new SynchronizedTest5();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
test5.methodA();
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
test5.methodB();
}
});
thread2.start();
}
}
运行结果:
methodA-0
methodB-0
methodA-1
methodB-1
methodB-2
methodA-2
methodB-3
methodA-3
methodB-4
methodA-4
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107763.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...