Accessing thread from called class

A

Angus

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
 
T

Thomas Hawtin

Angus said:
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
 
C

Chris

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.
 
A

Angus

Chris said:
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.


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
 
C

Chris

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?

Declare t as type ThreadNetworkHandler, not Thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top