• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing question , how to share a resource between thereads.

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
In a section of mmy application i have some threads which i start by a button.
I have a List object in my main class , in this list i store some objects that
my thread need to process , i should show users that which object is under process in my GUI
and also be able to stop the thread when user press a button.
what is best solution for this ?
specially for sharing that Objects list.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This gets a bit tricky but it's good clean fun. I'd imagine something like this:

A button click on your GUI starts a new thread/runnable, which pulls a task from the list and executes it. It would be good to have a controller in there somewhere so the thread startup code is not mixed up with GUI code.

The runnable wants to notify the GUI when it starts a task and when it finishes the task. Since it's on its own thread, it can't just update GUI components and expect them to paint correctly. It has to go through SwingUtilities to do the update. That involves yet another Runnable (that I didn't show) that updates the GUI component. If you read up on SwingUtilities.invoke*** methods you'll see how that hooks up.

Does that makes sense so far? I'm giving myself more time to think about stopping threads before answering. Depending on the task that ranges from tricky to much worse.

Edit: The Ranch sometimes doesn't display mono-fonts correctly on FireFox. If that diagram doesn't line up neat, copy-paste it to a text editor or code editor that uses a good mono-font.
[ March 17, 2006: Message edited by: Stan James ]
 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for the reply. Diagram showed correctly in FF 1.5.01.
My condition is like :


that sendLinePoints will send some point to a hardware , so i should wait for each call to send line points to complete
before i issue another call.

meanwhile i should do something like tho following to update my gui codes.
it is kinda seducode for now.






those updateGUI methods update some swing component to show current state and current line which is under process.
that doSerialPort check serial port to find out whether hardware send any command ,
checkforStopperButtonPressed check to see whether stop button is pressed or not.

Now i should find a way to do the following :
even when send Linepoints is processing , user pressed stop button i should stop the thread that
is executing that method. (also if there come a command on serial port i should be able to fetch and read that command and maybe stop the thread)


some of the prolem which i am engaged with :

when i use a thread to do sendLinePoints , thread goes to its way and for loop will create another thread for ext line
both of this thread are trying to send point and everything blow.
I should wait until each call to send line points ended and then call the method for next line
meanwhile i should be able to check for userinputs (buttons) and check for serial port.
(this means that GUI should not HANG)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see if I'm getting this right ...

The loop you show above runs in its own thread. When you hit the stop button you'd like to do the serial port check and exit the loop. Your loop will actually do that right now.

But! If the loop is in the middle of the call to sendLinePoints() the current code has to wait for sendLinePoints to return, which may be long enough for the user to notice some delay. You'd like to find some way to break out of sendLinePoints and make it return immediately.

Ok so far?

You'll need the stop button to call a method on this object that is looping. That shouldn't be too hard to make.

What that method does depends entirely on what sendLinePoints does. If it's doing blocking IO you may well be out of luck. I had some luck once closing a socket connection that was hanging to get an exception right away, but I don't think that is reliable. Can you share the sendLinePoints code? Maybe we'll come up with a way to break out of it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!