The Artima Developer Community
Sponsored Link

Java Answers Forum
Threads

3 replies on 1 page. Most recent reply: Jun 4, 2002 12:38 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Sony.C.joy

Posts: 7
Nickname: sonycjoy
Registered: Jun, 2002

Threads Posted: Jun 3, 2002 5:30 AM
Reply to this message Reply
Advertisement
How two threads will communicate each other ?
ie communication between threads


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Threads Posted: Jun 3, 2002 6:19 PM
Reply to this message Reply
What do you mean by communicate? Could you be more specific?

In the mean time search for "Programming Java threads in the real world" on http://www.JavaWorld.com -- there is a whole series of articles by Allen Holub which should answer all your question in detail.

Sony.C.joy

Posts: 7
Nickname: sonycjoy
Registered: Jun, 2002

Re: Threads Posted: Jun 3, 2002 9:28 PM
Reply to this message Reply
Communication means two threads running seperately wants to pass some information each other at different points.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Threads Posted: Jun 4, 2002 12:38 AM
Reply to this message Reply
Here is a simple example of two threads communicating asynchronously. The communication is one-way, to make it more clear to follow what is going on. The random timing allows you to run it several times to get an idea of how the communication proceeds when the sender and receiver work at different paces and for varying intervals.

import java.util.*;
 
public class TalkingThreads 
{
   public static void main( String [] args )
   {
      Listener listener = new Listener();
      Talker talker = new Talker(listener);
      talker.start();
      listener.start();
   }
 
}
class Talker extends Thread
{
   String [] thingsToSay = { "Hi", "How's it goin'?", "Hey there",
                             "How do?", "Howdy", "Felicitations", 
                             "Hiya", "Hey", "Yo", "Wutup?", "Hola",
                             "Bon Jour", "Guten Tag", "Previet" };
   private Listener listener;
   private static Random randy = new Random();
 
   public Talker( Listener l )
   {
      listener = l;
   }
 
   public void run()
   {
      long endTime = System.currentTimeMillis() + 5*1000 + randy.nextInt(10*1000);
 
      while( System.currentTimeMillis() < endTime  )
      {
         try 
         {
            Thread.sleep( randy.nextInt( 1000 ) + 100 );
         }
         catch( InterruptedException ie )
         {
            System.out.println("Leave me alone, I'm trying to sleep.");
         }
 
         talk( listener );
      }
      System.out.println( "Comment from Talker:  I've had enough of this conversation." );
   }
 
   public void talk( Listener l )
   {
      String said = thingsToSay[randy.nextInt( thingsToSay.length )];
      l.listen( said );
      System.out.println( "Comment from Talker:  I just said:  " + said );
   }
}
 
class Listener extends Thread
{
   private LinkedList messageQueue = new LinkedList();
   private static Random randy = new Random();
 
   synchronized public void listen( String msg )
   {
      messageQueue.add(msg);
   }
 
   synchronized public void echoMessage()
   {
      System.out.print( "Comment from Listner: " );
      if( messageQueue.isEmpty() )
         System.out.println( "Haven't heard much lately." );
      else
         System.out.println( "I just heard: " + (String)messageQueue.removeFirst() );
   }
 
   public void run()
   {
      // Only pay attention for a minute:
      long endTime = System.currentTimeMillis() + 7*1000 + randy.nextInt(15*1000);
 
      while( System.currentTimeMillis() < endTime  )
      {
         try
         {
            Thread.sleep( randy.nextInt( 1200 ) );
         }
         catch( InterruptedException ie )
         {
            System.out.println("Leave me alone, I'm trying to sleep.");
         }
 
         echoMessage();
      }
      System.out.println( "Comment from Listner: That's what I was going to say." );
   }
 
}

Flat View: This topic has 3 replies on 1 page
Topic: EJB Transaction rollback exception in Visual Age environment Previous Topic   Next Topic Topic: can you declare a method to be virtual in Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use