Java multi-threading - Synchronization: synchronized and thread communication: producer-consumer model

Big guy a nice weekend, we offer a small music came to dinner technology. Speaking last is a Java multi-threaded and create state | music bytes , then, let us then say that Java multi-threading - Synchronization: synchronized and thread communication: producer-consumer model

A synchronization: synchronized

Multiple threads simultaneously access an object that may cause non-thread-safe, data may be wrong, so-called synchronization: is to control multiple threads simultaneously visit is to control multi-threaded operating the same object, pay attention to the same object, data accuracy, ensure data security, but after adding sync because of the need to wait, so the efficiency is relatively low.

Such as: an apple, how to bite himself a man will not go wrong, but more than one person at the same time to bite, this time if not properly managed, it could bite each other anymore. Again is an apple.

12306 in the train, and why the time to grab votes, need to wait before locking seats payment? This is to ensure that a person to buy a ticket only.


1, sync block

Block synchronized +: sync blocks

synchronized (reference type | subject | class .class) {

}


2, synchronization method

Synchronized modifier return type | void method signature {

}

3, deadlock

Too easy synchronization deadlock

/**
* 死锁:容易造成死锁
* @author Administrator
*
*/
public class TestDeadLock {
/**
* @param args
*/
public static void main(String[] args) {
Object obj1 =new Object();
Object obj2 =new Object();
new A(obj1,obj2).start();
new B(obj1,obj2).start();
} }
class A extends Thread{
Object obj1 ;
Object obj2;
public A() {
}
public A(Object obj1, Object obj2) {
super();
this.obj1 = obj1;
this.obj2 = obj2;
}
@Override
public void run() {
synchronized(obj1){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(obj2){
} 
}
System.out.println("给我烟");
} }
class B extends Thread{
Object obj1 ;
Object obj2;
public B() {
}
public B(Object obj1, Object obj2) {
super();
this.obj1 = obj1;
this.obj2 = obj2;
}
@Override
public void run() {
synchronized(obj2){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(obj1){
} }
System.out.println("给我钱");
} 
}复制代码


Second, the thread communication: producer-consumer model

Target thread is to enable communication between threads can transmit signals to each other. On the other hand, so that the thread can wait thread communication which

Signal his thread.

Java has a built-in mechanism to allow threads to wait while waiting for signal becomes non-operational state.

java.lang.Object class defines three methods, wait (), notify () and notifyAll () wait mechanism to achieve this. Once a thread calls of any object wait () method, will become non-operational state, until another thread calls the same object notify () method. In order to call wait () or notify (), the thread must first acquire the lock that object. In other words, the thread must be called sync blocks in wait () or notify ().

The following is a use of the wait () and notify () to achieve the shared object between the threads of communication:

  /**
* 街道
* @author Administrator
*
*/
public class Street {
//红绿灯
//false 红灯 -->人等 车走 -->换灯 通知人走
//true 绿灯 -->车等 人走 -->换灯 通知车走
private boolean flag=false;
//东西向 -->人道
public synchronized void west(){
if(flag==false){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
} }
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("东西向-->人走");
//换灯
this.flag =false;
this.notify(); //唤醒等待者
}
//南北向 车道
public synchronized void north(){
if(flag==true){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
} }
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("南北向-->车走");
//换灯
this.flag =true;
this.notify(); //唤醒等待者
} }
class Person extends Thread {
private Street s ;
public Person(Street s) {
this.s = s; }
public void run() {
for(int i=0;i<10;i++){
s.west(); //东西向
}} }
class Car extends Thread {
private Street s ;
public Car(Street s) {
this.s = s; }
public void run() {
for(int i=0;i<10;i++){
s.north();
} 
} 
}复制代码

Java multi-threading and thread synchronization communication introduced here, please pay attention to dry more technical music bytes. Byte original music!


Guess you like

Origin juejin.im/post/5d4f905af265da03d871aef0