Java --- 17 --- communication between multiple threads, java --- 17 --- Multithreading

Source: Internet
Author: User

Java --- 17 --- communication between multiple threads, java --- 17 --- Multithreading

We have introduced the importance of using the synchronization mechanism in multi-threaded programming and learned how to implement synchronization to access Shared resources correctly. The relationships between these threads are equal and there is no dependency between batches. They compete for CPU resources and do not interact with each other. They also unconditionally prevent other threads from accessing shared resources. However, there are also many practical problems that require not only synchronous access to the same shared resource, but also mutual restraint between threads, through mutual communication to push forward. So how do multiple threads communicate?

Inter-thread communication means that multiple threads operate on the same resource, but the operations are different.


Class Res {String name; String sex;} class Input implements Runnable {private Res r; public Input (Res r) {// TODO Auto-generated constructor stubthis. r = r;} public void run () {int x = 0; while (true) {if (x = 0) {r. name = "mike"; r. sex = "man";} else {r. name = "Lili"; r. sex = "female";} x = ++ x % 2; }}} class Output implements Runnable {private Res r; Output (Res r) {this. r = r;} public void run () {while (true) {System. out. println (r. name + "----" + r. sex) ;}} public class Communicate {public static void main (String [] args) {// TODO Auto-generated method stubRes r = new Res (); input in = new Input (r); Output out = new Output (r); Thread t1 = new Thread (in); Thread t2 = new Thread (out); t1.start (); t2.start ();}}

However, the name does not match the gender.

This indicates that there is a security problem and synchronization is required when there is a security problem.

Add synchronization and try again:

Class Res {String name; String sex;} class Input implements Runnable {private Res r; Object obj = new Object (); public Input (Res r) {// TODO Auto-generated constructor stubthis. r = r;} public void run () {int x = 0; while (true) {synchronized (obj) {if (x = 0) {r. name = "mike"; r. sex = "man";} else {r. name = "Lili"; r. sex = "female";} x = ++ x % 2 ;}}} class Output implements Runnable {private Res r; Object obj = new Object (); output (Res r) {this. r = r;} public void run () {while (true) {synchronized (obj) {System. out. println (r. name + "----" + r. sex) ;}}} public class Communicate {public static void main (String [] args) {// TODO Auto-generated method stubRes r = new Res (); input in = new Input (r); Output out = new Output (r); Thread t1 = new Thread (in); Thread t2 = new Thread (out); t1.start (); t2.start ();}}

However, the problem still persists.

The two principles should be taken into consideration:

1. Is there two or more threads? (Satisfied)

2. Is it the same lock? (??)

 

This is a class object, so you don't have to think about it.


There are already four. class objects in the memory. Which one can be used.


Class Res {String name; String sex;} class Input implements Runnable {private Res r; public Input (Res r) {// TODO Auto-generated constructor stubthis. r = r;} public void run () {int x = 0; while (true) {synchronized (Input. class) {if (x = 0) {r. name = "mike"; r. sex = "man";} else {r. name = "Lili"; r. sex = "female";} x = ++ x % 2 ;}}} class Output implements Runnable {private Res r; Object obj = new Object (); output (Res r) {this. r = r;} public void run () {while (true) {synchronized (Input. class) {System. out. println (r. name + "----" + r. sex) ;}}} public class Communicate {public static void main (String [] args) {// TODO Auto-generated method stubRes r = new Res (); input in = new Input (r); Output out = new Output (r); Thread t1 = new Thread (in); Thread t2 = new Thread (out); t1.start (); t2.start ();}}

However, in this example, resource r is unique, that is, if the lock is changed to r, you can also.









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.