Sign in to follow this  
ajm113

[java] Multithreading Class Sending Data To The Main Class?

Recommended Posts

I'm new to Java and I haven't really tacked this type of issue, but since my source code is pretty big I'll break it down so it's very easy to understand my problem without using a lot of source code.

So I have a client/server merged into one app and made two classes. The 1st class is the main class which handles the frame and holds the variable of the 2nd class. This second class does networking and multithreading into one. So of course I can very easily pass variables from the main class to the second class in a variable form. But what if ... I want to lets say create a message dialog when ever a client connects in the second class of a thread. So when the user clicks okay I can access private/public variables and change their value of the main class? I was thinking of maybe creating a event listener in the main class that the second class can call and be able to pass values, but I'm not sure if thats the best option.

Thanks, Andrew.

P.S I'm using this multithreading type of class:

[code]
public class MyThread implements Runnable {

public void run() {
.....
}

}


public class DoNetworkingAndThreading
{
public void IDoSomeWork()
{
//Calls the MyThread in a separate thread...
}

}

[/code]

Share this post


Link to post
Share on other sites
You need to [url="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html"]find or write a thread safe communications mechanism to allow your networking thread to notify the UI of events[/url], such as a new client. You need to be extremely careful touching variables that can be accessed by more than one thread. You really need to understand the core principles of concurrent programming to get this right.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1313857199' post='4851636']
You need to [url="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html"]find or write a thread safe communications mechanism to allow your networking thread to notify the UI of events[/url], such as a new client. You need to be extremely careful touching variables that can be accessed by more than one thread. You really need to understand the core principles of concurrent programming to get this right.
[/quote]


Yeah as above threading and synchronization is quite complex. Here a link to what I would use for your problem:

[url="http://download.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html"]http://download.orac...y/syncmeth.html[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this