Welcome to Dream.In.Code
Become a Java Expert!

Join 149,516 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,344 people online right now. Registration is fast and FREE... Join Now!




Inter-Thread communication

 
Reply to this topicStart new topic

Inter-Thread communication

ap0c0lyps3
7 Jul, 2007 - 12:23 PM
Post #1

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
Ye'lo it's me again!

I have a program that consists of a server and a whole lot of clients. The server's only form of GUI is a button thingy on the System Tray. Heres a diagram that shows how the classes work.

Main.java -> Server.java -(doesn't keep reference)>serverThread.java

When the server person clicks "shutdown" on the gui(main.java) I want all the serverThreads to send a "SHUTDOWN" to all the clients. All I want to know is how do I get the message all the way across?
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Inter-Thread Communication
7 Jul, 2007 - 12:43 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you will have to link some part of the server threads such that all threads launch shutdown command to all client threads.
When your server creates it's server threads, it needs to keep a reference to the threads, or the threads need to continually check that the connection to original thread still exists.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Inter-Thread Communication
7 Jul, 2007 - 02:25 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I could imagine a solution, that would do the following: when the server thread is stopped, it would interrupt all the worker threads (no problem, that it doesn't have the references, they can be found via the thread and threadgroup classes), they would either get an interrupted exception (if blocked) or they would find out about this by checking the interrupted flag of the thread from time to time. Once they sense that they are interrupted, they would send the shutdown to the clients.

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Inter-Thread Communication
7 Jul, 2007 - 06:08 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
QUOTE(1lacca @ 7 Jul, 2007 - 06:25 PM) *

I could imagine a solution, that would do the following: when the server thread is stopped, it would interrupt all the worker threads (no problem, that it doesn't have the references, they can be found via the thread and threadgroup classes), they would either get an interrupted exception (if blocked) or they would find out about this by checking the interrupted flag of the thread from time to time. Once they sense that they are interrupted, they would send the shutdown to the clients.

this is a good option, but i assumed that it was a proper server client connection, which the processes may not only not be run within the same base thread, but not even on the same computer. Though the first attempt to access a closed server should also produce a blocked scenario.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Inter-Thread Communication
7 Jul, 2007 - 06:59 PM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I think the problem is not specified enoguh. I was assuming, that we are talking about a multithreaded server, that spawns a thread for each connection it establishes with the clients. I was referring to these working threads (that are in the same same process as the server), that would send the shutdown signal to the clients (assuming two way, connection based communicaiton) via TCP/IP or whatever medium the connection is based on.

Yes, too much assumptions, but this is the most common situation.
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Inter-Thread Communication
10 Jul, 2007 - 01:33 AM
Post #6

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
It is a multi-threaded server but it is on one PC. I am using TCP/IP.
I checked out the interrupt option before but what if I want to send
a different command to all the clients.

EDIT: Yes they are on the same process as well

This post has been edited by ap0c0lyps3: 10 Jul, 2007 - 01:37 AM
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Inter-Thread Communication
10 Jul, 2007 - 01:48 AM
Post #7

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
What is the easiest way to keep a reference of all the serverthreads?
A list or an array.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Inter-Thread Communication
10 Jul, 2007 - 03:56 AM
Post #8

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE
What is the easiest way to keep a reference of all the serverthreads?
A list or an array.

A Collection, so you can change the implementation in a jiffy (and test which one performs better).

QUOTE
I checked out the interrupt option before but what if I want to send
a different command to all the clients.

It is no problem, when the serverthread detects that it is interrupted, it can choose what message to send to the client it serves.

QUOTE

EDIT: Yes they are on the same process as well


As I've written it is no problem either, my wording was a bit messy, so William thought that I want to interrupt the clients themselves, and not the serverthreads that are serving them (or this is what I think he meant)

User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Inter-Thread Communication
10 Jul, 2007 - 07:40 AM
Post #9

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
Isn't there some way to make global variables in Java like you
can in PHP? Like you cxan access them from any class
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Inter-Thread Communication
10 Jul, 2007 - 08:33 AM
Post #10

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
static variables are just that. But what do you need them for?

This post has been edited by 1lacca: 10 Jul, 2007 - 08:33 AM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Inter-Thread Communication
10 Jul, 2007 - 11:05 AM
Post #11

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
QUOTE(1lacca @ 10 Jul, 2007 - 12:33 PM) *

static variables are just that. But what do you need them for?

yes i wonder this myself.
*As for before i had interpreted it as interrupting clients, but now that i read it... what you meant makes much more sense, lol smile.gif


If all server threads are spawned from a single .jar or .class file, they are all part of a singular process, terminating the root thread, and thus the process will interrupt and attempt to end the server threads. If you were to do this, and then perhaps do a join() or wait() for the threads to finish, before allowing the actual exit, it would give the threads time to process that they are being killed and thus should inform their clients.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:07PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month