• Post Reply Bookmark Topic Watch Topic
  • New Topic

listening to another thread  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have 2 threads , suppose t1 and t2,
and 2 windows related to each other, suppose w1 to t1 and w2 to t2.
none of these threads can be 'wait'ed or suspended.
I want t1 to do something when w2 is scrolled down or up.Ofcourse this event(Scrolling) happens in t2 and t2 hasn't access to do the needed job.
How t1 can understand when something happen in t2( here it is scrolling ) without being suspended?
Is there anyway that t1 listens to some event in t2 while it is doing it's own jobs?
I hope there is!

Thank you.
 
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several techniques to communicating between threads: pipes, shared objects, queues. If you are communicating between GUI classes, it may be easiest to use events. Either by using the Swing API events or java.util.Observable.
Now if you want the threads to feed events to each other, I usually handle that problem by implementing a queue and having the threads feed objects to each other in the typical producer-consumer pattern.
 
mohsen fakhari
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your full of informaation answer .

'If you are communicating between GUI classes...'
Yes I'm doing it and I'm using events.
I'm using SWT(eclipse java GUI library) and I want to listen to scroll event of an AWT scrollbar.This is when I encounter the problem.
I've made a AdjustmentListener and added it to the scrollbar (both in SWT, i.e. t1). But 'adjustmentValueChanged' method is running in AWT thread (i.e. t2) and this thread can't change a Tree created in SWT (t1).
Do you think I've made some mistakes in event structure or I should use another structure.

by the way I'm reading other topics you suggested
Thank you again
 
mohsen fakhari
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry , maybe it's so easy, but let me see
I create a AdjustmentListener and add it to a AWT ScrollBar (in t1 thread). Now the event happens (in t2 thread) and the fire... method in ScrollBar class calls adjustmentValueChanged method of all its listeners including my listener.
Now we are in t2 yet, aren't we?
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never done anything with SWT, but any event from AWT will be running on the AWT event handler thread. If you want to pass a task off to another thread you will have to use one of the techniques stated above.
The Java Tutorial chapter Concurrency in Swing describes how to deal with threads in the Swing library.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!