What is multithreaded communication?
Multithread communication means that multiple threads operate on a resource at the same time, but the actions are different.
code implementation
package com.kernel; class Res { private String name; private String sex; private Boolean flag; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } } class InputThread extends Thread { private Res res; public InputThread1(Res res) { this.res = res; } @Override public void run() { int count = 0; while (true) { synchronized (res) { if (res.getFlag()) { try { res.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } if (count == 0) { res.setName("Xiaohong"); res.setSex("female"); } else { res.setName("Small army"); res.setSex("male"); } count = (count + 1) % 2; res.setFlag(true); res.notify(); } } } } class OutputThread extends Thread { private Res res; public OutputThread1(Res res) { this.res = res; } @Override public void run() { while (true) synchronized (res) { if (!res.getFlag()) { try { res.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(res.getName() + "," + res.getSex()); res.setFlag(false); res.notify(); } } } public class Test002 { public static void main(String[] args) { Res res = new Res(); res.setFlag(false); InputThread inputThread = new InputThread(res); OutputThread outputThread = new OutputThread(res); inputThread.start(); outputThread.start(); } }
Why can't you use this.wait()?
Because the two threads created above were created by two classes, their objects are different, so the objects are different.
Why does wait and notify belong to Object?
That's because wait and notify need to use object locks. As we all know, all global objects can be used as object locks, while Object is the parent class of all objects. As long as wait and notify are implemented in Object, all classes can be used.
wait,notify
Because wait and notify use object locks, they must be used in synchronization
wait blocks currently executing threads
Notfy wakes up threads in the lock pool to run
The difference between wait and join
wait must be placed in synchronize
join does not need to wake up
The Difference between wait and sleep
sleep does not need to be placed in synchronize
sleep does not release locks
Lock lock
lock writing
Lock lock = new ReentrantLock(); lock.lock(); try{ //Thread-safe operations may occur } catch(abnormal){ //Handling exceptions } finally{ //Must release the lock in final //You can't lock acquisition in try either, because it's possible to throw an exception when acquiring a lock. lock.unlock(); }
The difference between Lock and Synchronsize
Lock can acquire locks without blocking. The current thread attempts to acquire locks. If locks are not acquired by other threads, locks are successfully acquired and held.
Lock interface can be interrupted to acquire locks. Threads that acquire locks can respond to interruptions. When the thread of acquired locks is interrupted, interruption exceptions will be thrown, and locks will be released.
Lock interface acquires the lock before the specified deadline, and returns if the deadline still fails to acquire the lock.
Condition usage
Condition s are equivalent to wait and notify functions
Condition condition = lock.newCondition();
res. condition.await(); similar to wait
res. Condition. Signal() is similar to notify
class Res { private String name; private String sex; private Boolean flag; Lock lock = new ReentrantLock(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } } class InputThread extends Thread { private Res res; Condition condition; public InputThread (Res res, Condition condition) { this.res = res; this.condition = condition; } @Override public void run() { int count = 0; while (true) { try { res.lock.lock(); if (res.getFlag()) { try { res.wait(); condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } if (count == 0) { res.setName("Xiaohong"); res.setSex("female"); } else { res.setName("Small army"); res.setSex("male"); } count = (count + 1) % 2; res.setFlag(true); condition.signal(); } catch (Exception e) { } finally { res.lock.unlock(); } } } } class OutputThread extends Thread { private Res res; Condition condition; public OutputThread (Res res, Condition condition) { this.res = res; this.condition = condition; } @Override public void run() { try { res.lock.lock(); while (true) { if (!res.getFlag()) { try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(res.getName() + "," + res.getSex()); res.setFlag(false); condition.signal(); } } catch (Exception e) { } finally { res.lock.unlock(); } } } public class Test003 { public static void main(String[] args) { Res res = new Res (); Condition condition = res.lock.newCondition(); res.setFlag(false); InputThread inputThread = new InputThread(res, condition); OutputThread outputThread = new OutputThread(res, condition); inputThread.start(); outputThread.start(); } }