Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Accessing thread from called class

Thread view: 
Angus - 25 Nov 2006 19:22 GMT
Hello

I have a Java class (an applet) which spins off a separate thread to handle
socket networking communication code.   I pass the this ref to the new spun
off thread so the thread class can communicate with the applet GUI.
However, if I for example, click on a button in the applet, I want to pass
the request onto the networking thread.

So questions are:

1. How do I invoke a method in the spun off thread from the applet?  My
thread looks like this:

// class ThreadNetworkhandler
class ThreadNetworkHandler extends Thread
{
private CubaThread m_Object;
// Constructor
ThreadNetworkHandler(CubaThread obj)
{
    m_Object = obj;
}

   public void run()
   {
           // do stuff here eg m_Object.lst.addItem("blah...");
    }
}

And I spin it off like this:

 t = new ThreadNetworkHandler(this);
 t.start();

Where t is of type Thread - a member variable of the applet class.

Do I just implement eg a SendCommand() function (or whatever name you like)
in the ThreadNetworkHandler class and call that?

2. Another question on same sort of topic.  Should I have one thread for
incoming socket communication stream and another for the outgoing?

Any feedback would be much appreciated.

Angus
Thomas Hawtin - 25 Nov 2006 19:54 GMT
> 1. How do I invoke a method in the spun off thread from the applet?  My
> thread looks like this:

Generally, have the thread pulling commands off of a blocking queue,
such as BlockingQueue.

> 2. Another question on same sort of topic.  Should I have one thread for
> incoming socket communication stream and another for the outgoing?

Generally a good idea. With protocol which have an idea of whose turn is
next, such as HTTP, you can get away with one thread.

Tom Hawtin
Chris - 25 Nov 2006 22:14 GMT
> Do I just implement eg a SendCommand() function (or whatever name you like)
> in the ThreadNetworkHandler class and call that?

Yes, that will work. Make sure that you synchronize access to any
variables you're setting, though.

String command;

public synchronized void sendCommand(String command) {
    this.command = command;
}
private synchronized getCommand() {
    return command;
}

public void run() {
    // main loop
    while (true) {
        String command = getCommand(); // this is synchronized
    }
}

Actually, synchronization probably isn't technically required in the
example above (because assigning a String is atomic), but if you're
doing anything more complicated in sendCommand() it's a good idea.

> 2. Another question on same sort of topic.  Should I have one thread for
> incoming socket communication stream and another for the outgoing?

Take a look at NIO. Can be helpful when there are many threads and good
performance is required. Not otherwise necessary, though.
Angus - 26 Nov 2006 17:39 GMT
> > Do I just implement eg a SendCommand() function (or whatever name you like)
> > in the ThreadNetworkHandler class and call that?
[quoted text clipped - 27 lines]
> Take a look at NIO. Can be helpful when there are many threads and good
> performance is required. Not otherwise necessary, though.

I have this class:

// class ThreadNetworkhandler
class ThreadNetworkHandler extends Thread
{
private CubaThread m_Object;
// Constructor
ThreadNetworkHandler(CubaThread obj)
{
m_Object = obj;
}

String command;

public synchronized void sendCommand(String command)
{
    this.command = command;
}
private synchronized String getCommand()
{
    return command;
}

   public void run()
   {
         try
   {
    for (;;)
        {
              Toolkit.getDefaultToolkit().beep();
              Thread.currentThread().sleep(2000);

              String command = getCommand(); // this is synchronized
              m_Object.lst.addItem("You issued: " + command);
          }
   }
   catch(java.lang.InterruptedException e)
   { /* no problem, end of wait */
      }

}
}

In my applet I do this:

 t = new ThreadNetworkHandler(this);
 t.start();

Where t is a member variable of type Thread.

But if I do this:

t.sendCommand("My Command\r\n");

I get this error:

CubaThread.java:44: cannot find symbol
symbol  : method sendCommand(java.lang.String)
location: class java.lang.Thread
                 t.sendCommand("My command\r\n");

So how do I call sendCommand?

Angus
Chris - 26 Nov 2006 21:01 GMT
> In my applet I do this:
>
[quoted text clipped - 15 lines]
>
> So how do I call sendCommand?

Declare t as type ThreadNetworkHandler, not Thread.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.