Java---the communication between---multithreading

Source: Internet
Author: User

In the previous section, we have introduced the importance of using synchronization mechanisms in multithreaded programming, and learned how to implement synchronous methods to properly access shared resources. The relationships between these threads are equal, there is no dependency between batches, they compete with each other for CPU resources, and they also unconditionally block access to shared resources by other threads. However, there are a lot of real-world problems that require not only simultaneous access to the same shared resources, but also threads that are pinned to each other and pushed forward by communicating with each other. So how do you communicate between multiple threads?

The communication between threads is actually that multiple threads are manipulating the same resource, but the actions 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 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 (); Inpu T in = new Input (r), output out = new output (r); thread T1 = new thread (in); Thread t2 = new Thread (out); T1.start (); T2.start ();}}

But it would appear that the name does not match the gender.

This indicates a security issue that requires synchronization when security issues arise.

Let's add the synchronization and try again:

Class Res {String name; String sex;} Class Input implements Runnable {private Res r;object obj = new Object ();p ublic Input (Res R) {//TODO auto-generated const Ructor 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 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 (); Inpu T in = new Input (r), output out = new output (r); thread T1 = new thread (in); Thread t2 = new Thread (out); T1.start (); T2.start ();}}

But this did not solve the problem.

Then there are two principles to consider:

1. are two and more than two threads? Meet

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

This is the object of this class, not to mention it.


There are already four. class objects in memory, which 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 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 (); Inpu T in = new Input (r), output out = new output (r); thread T1 = new thread (in); Thread t2 = new Thread (out); T1.start (); T2.start ();}}

But in this case, the resource R is also unique, that is, if the lock is converted to R.









Java---the communication between---multithreading

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.