Java thread synchronization mechanism, keywords synchronized, wait, notify/notifyAll(), join, Lock. Among them, wait, notify/notifyAll() must be used with the synchronized keyword.
The synchronized keyword can be applied to code blocks, methods, static methods, and a class.
Wait is to let the current thread enter the wait state and release the lock it holds until other threads call the notify() method or notifyAll() method of this object.
join causes the current thread to be abandoned and the corresponding thread to be returned. The main thread waits for the sub thread to finish executing.
Lock is an optimization of synchronized.
Direct code
- synchronized and wait, notify
import com.moc.leijian.tools.MOCUtil; /** * @author leijian Create on 2018 9:31:28 PM, March 10, 2010 * @since JDK1.8 * */ public class WaitDemo { private static Object lock = new Object(); public static void main(String[] args) throws InterruptedException { Thread notifyThread = new Thread(() -> { synchronized (lock) { MOCUtil.LogUtil("Start: Notify"); lock.notify(); MOCUtil.LogUtil("End: Notify"); } }); Thread waitThread = new Thread(() -> { synchronized (lock) { MOCUtil.LogUtil("Start: Wait"); try { lock.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { MOCUtil.LogUtil("End: Wait"); } } }); waitThread.start(); //waitThread.join(); notifyThread.start(); } } class NotifyThread implements Runnable { private Object lock; public NotifyThread(Object lock) { this.lock = lock; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override public void run() { synchronized (lock) { MOCUtil.LogUtil("Start:" + this.getClass().getName()); lock.notify(); MOCUtil.LogUtil("End:" + this.getClass().getName()); } } } class WaitThread implements Runnable { private Object lock; public WaitThread(Object lock) { this.lock = lock; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override public void run() { synchronized (lock) { MOCUtil.LogUtil("Start:" + this.getClass().getName()); try { lock.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { MOCUtil.LogUtil("End:" + this.getClass().getName()); } } } }
- Thread pool and Lock
import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.ReentrantLock; import com.moc.leijian.tools.MOCUtil; /** * There are three threads. How to make them execute in the order of 1, 2, 3 * * @author leijian Create on 2018 6:16:21 PM, March 10, 2010 * @since JDK1.8 * */ public class ThreadDemo { private static ReentrantLock lock = new ReentrantLock(); private static ArrayList<Integer> arrayList = new ArrayList<Integer>(); public static void main(String[] args) { // Create a one line pool implementation ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(() -> { MOCUtil.LogUtil("1:" + Thread.currentThread().getName()); }); executorService.execute(() -> { MOCUtil.LogUtil("2:" + Thread.currentThread().getName()); }); executorService.execute(() -> { MOCUtil.LogUtil("3:" + Thread.currentThread().getName()); }); executorService.shutdown(); } public static void insert(Thread thread) { //Code synchronization with Lock lock.lock(); try { for (int i = 0; i < 5; i++) { MOCUtil.LogUtil(thread.getName() + "Got the lock."); arrayList.add(i); } } catch (Exception e) { // TODO: handle exception } finally { MOCUtil.LogUtil(thread.getName() + "Release the lock."); lock.unlock(); } } }
- join case
import com.moc.leijian.tools.MOCUtil; /** * * @author leijian Create on 2018 10:38:00 p.m., March 10, 2010 * @since JDK1.8 * */ public class JoinTest { public static void main(String[] args) throws InterruptedException { Thread AliThread = new Thread(() -> { MOCUtil.LogUtil(Thread.currentThread().getName()); }, "Ali"); Thread HSFThread = new Thread(() -> { MOCUtil.LogUtil(Thread.currentThread().getName()); }, "HSF"); Thread TomcatThread = new Thread(() -> { MOCUtil.LogUtil(Thread.currentThread().getName()); }, "Tomcat"); /** * join It means to abandon the execution of the current thread and return the corresponding thread. For example, the following code means: * The program invokes the join method of the Ali thread in the main thread, and the main thread abandonment cpu control and returns Ali. The order of thread execution is Ali - > Tomcat HSF */ AliThread.start(); AliThread.join(); TomcatThread.start(); TomcatThread.join(); HSFThread.start(); } }
Welcome to WeChat official account.