Thread Synchronization & thread pool
thread synchronization
There will be no thread synchronization issues:
- When multiple threads operating the same resource, there will be problems and repeat the operation and operational resources do not exist, in order to circumvent this problem will require synchronization of threads to achieve the common use of resources.
Thread Synchronization:
- When multiple threads operating the same resource, the resource to the operation code plus a lock, when there is a thread to get the lock, other threads can not operate source code locked until the thread releases the lock to get lock.
3 ways to achieve thread synchronization:
Sync block
synchornized (obj) {
// code that relates to the operation of the same resource
}
Note: No static sync block
obj: any type of object, corresponds to a lock, the operation of the same resource must use the same thread lock.
How to ensure that multiple threads using the same lock?
In setting the thread task of defining thread object member variable declarations in the position of an object on the variable declaration in sychornized code block
/ **
analog functions tickets, open multiple threads simultaneously buy one hundred tickets
/
class SaleTicket the implements the Runnable {public
/
different objects with the same variable
/
Private static int Ticket = 100;/** *不同线程使用同一个锁对象 */ private Object obj = new Object(); @Override public void run() { while(true) { /** * 使用synchornized(obj) {} 代码块 */ synchronized (obj) { if (ticket > 0) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "sale=====" + ticket); ticket--; } } } } } public class SaleTicketDemoTest { public static void main(String[] args) { SaleTicket sale = new SaleTicket(); //开启3个线程操作同一对象,从而操作同一资源 Thread th1 = new Thread(sale); Thread th2 = new Thread(sale); Thread th3 = new Thread(sale); th1.start(); th2.start(); th3.start(); } }
The synchronization method
public synchornied void method name () {
Method // operations involving the same resource
}
lock synchronization process:
lock synchronization method is used in this object, i.e. new Thread (Xxx) is Xxx objects
package com.test.java;/** *模拟买票功能,开启多个线程,同时买一百张票 */ public class SaleTicket implements Runnable{ /* 不同对象使用同一个变量 */ private static int ticket=100 ; /** *不同线程使用同一个锁对象 */ private Object obj = new Object(); @Override public void run() { while(true) { saleTicket(); } } /** * 定义个个同步方法 public snychonized void XXX(){} */ public synchronized void saleTicket() { if (ticket > 0) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "sale=====" + ticket); ticket--; } } }
Static synchronization method
public static sychornized XXX () {
// code that relates to the operation of the common resources
}
static lock synchronization process is that an object class name .class
package com.test.java;/** *模拟买票功能,开启多个线程,同时买一百张票 */ public class SaleTicket implements Runnable{ /* 不同对象使用同一个变量 */ private static int ticket=100 ; /** *不同线程使用同一个锁对象 */ private Object obj = new Object(); @Override public void run() { while(true) { saleTicket(); } /** * 静态同步方法 */ public static synchronized void saleTicket() { if (ticket > 0) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "sale=====" + ticket); ticket--; } } }
Lock mechanism
The benefits of lock: shared resources to solve the problem when the lock has not been released, the other threads wait indefinitely.
after Jdk1.5 in java.util.concurrent.locks
When multiple threads are only read, so when a thread is performing a read operation, other threads can only wait can not be read. Therefore we need a mechanism to enable multiple threads are only read, not conflict between threads, you can do so by Lock
difference Lock and synchornized of:- Lock is not the Java language built-in, synchronized keyword Java language and is therefore built-in features. Lock is a class that can be synchronized access through this class;
- Lock and synchronized it is very much different, with synchronized without requiring the user to manually release the lock, when synchronized method or synchronized block of code executed, the system will automatically make the thread releases the lock of the occupation; and Lock must be user to manually release the lock, if not take the initiative to release the lock, it may lead to a deadlock.
- Using Lock, we must take the initiative to release the lock, and when an exception occurs, does not automatically release the lock. Therefore, in general, we must use Lock} catch {} block in try {, and the lock release operation is performed in the finally block to ensure that the lock must have been released, to prevent deadlocks. Lock commonly used to synchronize the words
- Lock is not the Java language built-in, synchronized keyword Java language and is therefore built-in features. Lock is a class that can be synchronized access through this class;
Thread Pool