java 多线程同步

一.synchronized关键字

同步方法

每个对象都包含一把锁(也叫做监视器),它自动称为对象的一部分(不必为此写任何特殊的代码)。调用任何synchronized方法时,对象就会被锁定,不可再调用那个对象的其他任何synchronized方法,除非第一个方法完成了自己的工作。

示例代码如下:

public class SimpleThread implements Runnable {

	private int count = 0;
	
	@Override
	public void run() {
		while (true) {
			try {
				test();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private synchronized void test() throws InterruptedException {
		System.out.println(Thread.currentThread().getName() + ":" + count++);
		Thread.sleep(100);
	}

	public static void main(String[] args) {
		SimpleThread sd = new SimpleThread();
		for (int i = 0; i < 3; i++) {
			new Thread(sd).start();
		}
	}

}

同步块

在进入同步块之前,必须在synchObject上取得锁。如果已有其他线程取得了这把锁,块便不能进入,必须等候那把锁被释放。一般情况下,当前对象作为锁来使用。

示例代码如下:

public class SimpleThread implements Runnable {

	private int count = 0;
	
	@Override
	public void run() {
		while (true) {
			try {
				test();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void test() throws InterruptedException {
		synchronized (this) {
			System.out.println(Thread.currentThread().getName() + ":" + count++);
		}
		
		Thread.sleep(100);
	}

	public static void main(String[] args) {
		SimpleThread sd = new SimpleThread();
		for (int i = 0; i < 3; i++) {
			new Thread(sd).start();
		}
	}

}

注:同步是一种高开销的操作,因此应该尽量减少同步的内容。 通常没有必要同步整个方法,使用synchronized代码块同步关键代码即可。

二.使用特殊域变量(volatile)

volatile关键字为域变量的访问提供了一种免锁机制,使用volatile修饰域相当于告诉虚拟机该域可能会被其他线程更新, 因此每次使用该域就要重新计算,而不是使用寄存器中的值,volatile不会提供任何原子操作,它也不能用来修饰final类型的变量。

public class SimpleThread implements Runnable {

	private volatile int count = 0;
	
	@Override
	public void run() {
		while (true) {
			try {
				test();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void test() throws InterruptedException {
		System.out.println(Thread.currentThread().getName() + ":" + count++);
		
		Thread.sleep(100);
	}

	public static void main(String[] args) {
		SimpleThread sd = new SimpleThread();
		for (int i = 0; i < 3; i++) {
			new Thread(sd).start();
		}
	}

}

注:由于线程执行速度不一样,所以控制台打印的结果不全是按从大到小来的。

三.使用java.util.concurrent包

JavaSE5.0中新增了一个java.util.concurrent包来支持同步。ReentrantLock类是可重入、互斥、实现了Lock接口的锁,它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力。

示例代码如下:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SimpleThread implements Runnable {

	private int count = 0;
	
	private Lock lock = new ReentrantLock();
	
	@Override
	public void run() {
		while (true) {
			try {
				test();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void test() throws InterruptedException {
		lock.lock();
		try {
			System.out.println(Thread.currentThread().getName() + ":" + count++);
		} finally {
			lock.unlock();
		}
		
		Thread.sleep(100);
	}

	public static void main(String[] args) {
		SimpleThread sd = new SimpleThread();
		for (int i = 0; i < 3; i++) {
			new Thread(sd).start();
		}
	}

}

四.使用ThreadLocal管理变量

如果使用ThreadLocal管理变量,则每一个使用该变量的线程都获得该变量的副本,副本之间相互独立,这样每一个线程都可以随意修改自己的变量副本,而不会对其他线程产生影响。

示例代码如下:

public class SimpleThread implements Runnable {

	private ThreadLocal<Integer> count = new ThreadLocal<Integer>() {

		@Override
		protected Integer initialValue() {
			
			return 0;
			
		}
		
	};
	
	
	@Override
	public void run() {
		while (true) {
			try {
				test();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void test() throws InterruptedException {
		System.out.println(Thread.currentThread().getName() + ":" + count.get().toString());
		count.set(count.get() + 1);
		
		Thread.sleep(100);
	}

	public static void main(String[] args) {
		SimpleThread sd = new SimpleThread();
		for (int i = 0; i < 3; i++) {
			new Thread(sd).start();
		}
	}

}
原文地址:https://www.cnblogs.com/minisculestep/p/4984346.html