Synchronized是java的关键字,是一种同步锁,它有一下几种使用方法:
1、作用于一个代码块,这个代码块是一个同步语句块,同一个对象同一时间只能允许在一个线程中执行。
2、作用于方法,同一个对象同一时间只能允许在一个线程中调用。
3、作用于静态方法,所有类对象同一时间只能允许在一个线程中调用。
4、作用于类,所有类对象同一时间只能允许在一个线程中调用。
作用于代码块
/**
* 同步线程
*/
class SyncThread implements Runnable {
private static int count = 0;
public void run() {
synchronized(this) {
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + ":" + (count++));
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class SynClass{
public static void main(String[] args) {
SyncThread syncThread = new SyncThread();
Thread thread1 = new Thread(syncThread, "SyncThread1");
Thread thread2 = new Thread(syncThread, "SyncThread2");
thread1.start();
thread2.start();
}
}
结果:
线程2等待线程1执行完才开始执行
作用于方法
/**
* 同步线程
*/
class SyncFunThread implements Runnable {
private static int count = 0;
public void run() {
if(Thread.currentThread().getName().equals("A")) {
fun1();
}else if(Thread.currentThread().getName().equals("B")) {
fun2();
}
}
private void fun1() {
synchronized(this) {
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + ":" + (count++));
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void fun2() {
for (int i = 0; i < 5; i++) {
try {
System.out.println("线程"+Thread.currentThread().getName()+"调用非同步函数");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class SynFunClass{
public static void main(String[] args) {
SyncFunThread syncThread = new SyncFunThread();
Thread thread1 = new Thread(syncThread, "A");
Thread thread2 = new Thread(syncThread, "B");
thread1.start();
thread2.start();
}
}
结果:
从结果来看,同一时间同一个对象只允许某一个线程执行同步函数,但是允许其他线程执行对象的非同步函数。
作用于静态方法
/**
* 同步线程
*/
class SyncFunThread implements Runnable {
private static int count = 0;
public void run() {
if(Thread.currentThread().getName().equals("A")) {
fun3();
}else if(Thread.currentThread().getName().equals("B")) {
fun3();
}
}
private synchronized static void fun3() {
for (int i = 0; i < 5; i++) {
try {
System.out.println("线程"+Thread.currentThread().getName()+"调用同步静态函数");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class SynFunClass{
public static void main(String[] args) {
SyncFunThread syncThread1 = new SyncFunThread();
SyncFunThread syncThread2 = new SyncFunThread();
Thread thread1 = new Thread(syncThread1, "A");
Thread thread2 = new Thread(syncThread2, "B");
thread1.start();
thread2.start();
}
}
结果:
两个对象同一时间只允许一个调用同步静态函数。
作用于类
/**
* 同步线程
*/
class SyncFunThread implements Runnable {
private static int count = 0;
public void run() {
if(Thread.currentThread().getName().equals("A")) {
fun4();
}else if(Thread.currentThread().getName().equals("B")) {
fun4();
}
}
private static void fun4() {
synchronized(SyncFunThread.class) {
for (int i = 0; i < 5; i++) {
try {
System.out.println("线程"+Thread.currentThread().getName()+"调用同步静态函数");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class SynFunClass{
public static void main(String[] args) {
SyncFunThread syncThread1 = new SyncFunThread();
SyncFunThread syncThread2 = new SyncFunThread();
Thread thread1 = new Thread(syncThread1, "A");
Thread thread2 = new Thread(syncThread2, "B");
thread1.start();
thread2.start();
}
}
结果:
线程A和线程B只允许一个执行函数fun4