Alternating printing of communication problems between multiple threads

Source: Internet
Author: User

1. To achieve the effect, direct:


1. The specific logic is clear, is through multithreading to achieve the direct code, lock control the same input or output thread synchronization,resource control the input and output thread synchronization

Class Resource{private String Name;private string sex;private boolean flag;public void SetName (String name) {this.name= Name;} public void Setsex (String sex) {this.sex=sex;} public void Setflag (Boolean flag) {This.flag=flag;} Public String GetName () {return this.name;} Public String Getsex () {return this.sex;} public Boolean Getflag () {return this.flag;}} Class Input implements Runnable{private static final object Lock=new object (); int num=0; Resource resource;public Input (Resource Resource) {This.resource=resource;} @Overridepublic void Run () {//TODO auto-generated method Stubwhile (! Thread.interrupted ()) {//Call interrupt method when terminating thread synchronized (lock) {synchronized (Resource) {if (Resource.getflag ()) {try {resource.wait ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if ((num&1) ==0)//With an odd even number to achieve alternating output {resource.setsex ("male"); Resource.setname ("Mnmlist");} Else{resource.setsex ("female"); Resource.setname ("Sting");} Num++;if (num>10000) Thread.CurrentThread (). Interrupt (); Resource.setflag (true); Resource.notify ();}}}} Class Output implements Runnable{private static final object Lock=new object (); int num=0; Resource resource;public Output (Resource Resource) {This.resource=resource;} @Overridepublic void Run () {//TODO auto-generated method Stubwhile (! Thread.interrupted ()) {synchronized (lock) {synchronized (Resource) {if (!resource.getflag ()) {try {resource.wait ()}; catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} System.out.println (Thread.CurrentThread (). GetName () + "," + (++num) + "th, Name:" +resource.getname () + ", Gender:" + Resource.getsex ()), if (num>10000) Thread.CurrentThread (). interrupt (); Resource.setflag (false); Resource.notify ( );}}}}} public class Oneafteranotheroutput {public static void main (string[] args) {//TODO auto-generated method Stubresource Res Ource=new Resource (); input input=new input (Resource); output output=new output (Resource); input input1=new input ( Resource); Output output1=new output (Resource); Thread inputthread=nEW Thread (input); Thread outputthread=new thread (output); Thread inputthread1=new thread (INPUT1); Thread outputthread1=new thread (OUTPUT1); Inputthread.start (); Outputthread.start (); Inputthread1.start (); O Utputthread1.start ();}}


Alternating printing of communication problems between multiple threads

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.