aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing worker thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing worker thread" Watch "Swing worker thread" New topic
Author

Swing worker thread

wayne whitman
Greenhorn

Joined: Sep 14, 2008
Posts: 10
Java newbie here with a worker thread problem.

I'm trying to receive streaming audio via UPD and when the program enters the for loop in the worker thread, it hangs and the event thread becomes unresponsive.
If I comment out the for loop, all is ok. Commenting out the audioPlayer line with the for loop uncommented makes no difference as long as the for loop is there. It seems like something with the loop itself.

Shouldn't I be able to run a long loop in the worker thread without disrupting the event thread?



Any advice appreciated and thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Indeed you should. But from the code you posted I don't see any evidence that you didn't run it in the event thread. Which it sounds like you did.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

How do you call this swing worker? And what does playAudio do?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
wayne whitman
Greenhorn

Joined: Sep 14, 2008
Posts: 10
Thanks folks, you must be correct about the thread, but I can't see why it's not executing in the worker thread.

Here's the calling code:


The playAudio ultimately tries to write to a sourceDataLine.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
wayne whitman wrote:Here's the calling code:



Yikes, calling doInBackground directly is just like calling run on a standard thread: nothing gets done on a background thread in either case. For a SwingWorker you must call execute() (have you read the tutorial -- it's in there), and for a standard thread, you call start().
wayne whitman
Greenhorn

Joined: Sep 14, 2008
Posts: 10
Thanks Pete - I'm rethinking the approach. But I do see examples in the Swing tutorial using .start() to invoke the background thread.

I'll keep working on it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You can compare SwingWorker to Thread: calling doInBackground() is just like calling run() - no new thread, just all of the work in the calling thread. execute() and start() are the means for starting a new thread that will do the work.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
wayne whitman wrote:Thanks Pete - I'm rethinking the approach. But I do see examples in the Swing tutorial using .start() to invoke the background thread.

That's the Thread tutorial. Have you read the SwingWorker tutorial. That's the one you need to look at now.
wayne whitman
Greenhorn

Joined: Sep 14, 2008
Posts: 10
I'm really feeling like a greenhorn now. I reread some of the Swing tutorial to try and understand this better.
Made a little progress. Took the following example from the Sun Swing tutorial. Looked like a good approach.
When I tried using this approach in my code, I keep getting "is not abstract and does not override abstract method..." error at the worker = new SwingWorker() { line.
The funny thing is when I compile the example source on the command line, it compiles ok, but when I put it into a class as an example project in NetBeans, it gets the same error as when I try to incorporate it into my code.
Can anyone explain what is going on?
Why does the example compile on the command line ok, but gives the override abstract method error when you put the same code into NetBeans? (I only left out the Swing component code from the example)

Thanks for your advice, again!



pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
What version of Java are you using? If 1.6, there's a newer version of SwingWorker that is different than what you are posting.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Swing worker thread