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