Java Threads (2)-Inter-thread communication

Source: Internet
Author: User

Method one by accessing shared variables (Note: Synchronization issues need to be handled)
Method two through the pipeline flow

There are two methods of implementation, namely
Method one a) implements shared variables for threads through an inner class

 Public classInnersharethread { Public Static voidMain (string[] args) {Mythread Mythread=NewMythread ();             Mythread.getthread (). Start ();             Mythread.getthread (). Start ();             Mythread.getthread (). Start ();         Mythread.getthread (). Start (); }     }     classMythread {intindex = 0; Private classInnerthreadextendsThread { Public synchronized voidrun () { while(true) {System.out.println (Thread.CurrentThread (). GetName ()+ "is running and index is" + index++); }             }         }               PublicThread GetThread () {return NewInnerthread (); }     }       /*** Implement shared variables of threads through internal classes **/  Public classInnersharethread { Public Static voidMain (string[] args) {Mythread Mythread=NewMythread ();          Mythread.getthread (). Start ();          Mythread.getthread (). Start ();          Mythread.getthread (). Start ();      Mythread.getthread (). Start (); }  }  classMythread {intindex = 0; Private classInnerthreadextendsThread { Public synchronized voidrun () { while(true) {System.out.println (Thread.CurrentThread (). GetName ()+ "is running and index is" + index++); }          }      }          PublicThread GetThread () {return NewInnerthread (); }  }
View Code

b) shared variables for threads implemented by implementing the Runnable interface

 Public classInterfacaesharethread { Public Static voidMain (string[] args) {Mythread Mythread=NewMythread (); NewThread (Mythread). Start (); NewThread (Mythread). Start (); NewThread (Mythread). Start (); NewThread (Mythread). Start (); }} /*implementing the Runnable interface*/classMythreadImplementsRunnable {intindex = 0;  Public synchronized voidrun () { while(true) System.out.println (Thread.CurrentThread (). GetName ()+ "is running and the index is" + index++);    }} /*** Implement shared variables for threads by implementing the Runnable interface*/ Public classInterfacaesharethread { Public Static voidMain (string[] args) {Mythread Mythread=NewMythread (); NewThread (Mythread). Start (); NewThread (Mythread). Start (); NewThread (Mythread). Start (); NewThread (Mythread). Start (); }} /*implementing the Runnable interface*/classMythreadImplementsRunnable {intindex = 0;  Public synchronized voidrun () { while(true) System.out.println (Thread.CurrentThread (). GetName ()+ "is running and the index is" + index++);    }}
View Code

Method two (through pipeline flow):

 Public classcommunicatewhitpiping { Public Static voidMain (string[] args) {/*** Create pipeline output stream*/PipedOutputStream POS=NewPipedOutputStream (); /*** Create a pipeline input stream*/PipedInputStream PiS=NewPipedInputStream (); Try {            /*** Connecting the pipeline stream to the output this process can also be implemented by overloading the constructor*/Pos.connect (PiS); } Catch(IOException e) {e.printstacktrace (); }        /*** Create a producer thread*/Producer P=NewProducer (POS); /*** Create consumer threads*/Consumer C=NewConsumer (PiS); /*** Start Thread*/P.start ();    C.start (); }} /*** Producer thread (associated with a pipeline input) **/classProducerextendsThread {PrivatePipedOutputStream POS;  PublicProducer (PipedOutputStream pos) { This. pos =POS; }      Public voidrun () {inti = 8; Try{pos.write (i); } Catch(IOException e) {e.printstacktrace (); }    }} /*** Consumer thread (associated with a pipeline input) **/classConsumerextendsThread {PrivatePipedInputStream PiS;  PublicConsumer (PipedInputStream PiS) { This. PiS =PiS; }      Public voidrun () {Try{System.out.println (Pis.read ()); } Catch(IOException e) {e.printstacktrace (); }    }}
View Code

Java Threads (2)-Inter-thread communication

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.