JAVA synchronized与lock的区别及多线程的同步和通信

1.synchronized与lock的区别

   synchronized是java内置的关键字,Lock是jdk1.5提供的java.util.concurrent包中提供的锁机制。 synchronized在代码执行完成之后会自动释放锁,lock机制需要手动调用unLock方法释放锁。高并发环境下synchronized会导致性能严重下降,这个时候推荐使用Lock( 实现类 ReentrantLock)。

2.多线程的同步与通信

  三个线程按照前后顺序依次打印threadname,打印十次

 a.synchronized 的实现

package com.cc.JUC.cnet;


public class SnycDemo {
	
	private volatile String id = "A";

	public synchronized void loopThread(int loopNum) {
		String name = Thread.currentThread().getName();
		try {
			while (!name.equals(id)) {
 			  this.wait();
			}
			
			System.out.println(Thread.currentThread().getName());
			this.notifyAll();
			if (name.equals("A")) {
				id = "B";
			}
			if (name.equals("B")) {
				id = "C";
			}
			if (name.equals("C")) {
				id = "A";
				System.out.println("--------------");
			}
		} catch (Exception ex) {
			System.out.println(ex.getLocalizedMessage());
		} 

	}
}

 

package com.cc.JUC.cnet;

public class TestDemo {

	public static void main(String[] args) {
		final SnycDemo demo = new SnycDemo();
         final int loopNum=10;
		new Thread(new Runnable() {
			public void run() {

				for (int i = 1; i <=loopNum; i++) {
					demo.loopThread(i);
				}

			}
		}, "A").start();
		new Thread(new Runnable() {
			public void run() {

				for (int i = 1; i <=loopNum; i++) {
					demo.loopThread(i);
				}

			}
		}, "B").start();
		new Thread(new Runnable() {
			public void run() {

				for (int i = 1; i <=loopNum; i++) {
					demo.loopThread(i);
				}

			}
		}, "C").start();

	}

}

 输出结果:JAVA synchronized与lock的区别及多线程的同步和通信_第1张图片

b.lock的实现

package com.cc.JUC.cnet;

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

public class AlternateDemo {
	private volatile String id = "A";

	private Lock lock = new ReentrantLock();
	private Condition cond = lock.newCondition();

	public void loopThread(int loopNum) {
		lock.lock();
		String name = Thread.currentThread().getName();
		try {
			while (!name.equals(id)) {
				cond.await();
			}
			System.out.println(Thread.currentThread().getName());
			cond.signalAll();
			if (name.equals("A")) {
				id = "B";
			}
			if (name.equals("B")) {
				id = "C";
			}
			if (name.equals("C")) {
				id = "A";
				System.out.println("--------------");
			}
		} catch (Exception ex) {
			System.out.println(ex.getLocalizedMessage());
		} finally {
			lock.unlock();
		}

	}

}
package com.cc.JUC.cnet;

public class TestDemo {

	public static void main(String[] args) {
		final AlternateDemo demo = new AlternateDemo();
         final int loopNum=10;
		new Thread(new Runnable() {
			public void run() {

				for (int i = 1; i <=loopNum; i++) {
					demo.loopThread(i);
				}

			}
		}, "A").start();
		new Thread(new Runnable() {
			public void run() {

				for (int i = 1; i <=loopNum; i++) {
					demo.loopThread(i);
				}

			}
		}, "B").start();
		new Thread(new Runnable() {
			public void run() {

				for (int i = 1; i <=loopNum; i++) {
					demo.loopThread(i);
				}

			}
		}, "C").start();

	}

}

 

输出结果:JAVA synchronized与lock的区别及多线程的同步和通信_第2张图片

 

 

你可能感兴趣的:(java)