• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing worker thread

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you call this swing worker? And what does playAudio do?
 
wayne whitman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
We're all out of roofs. But we still have tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic