0

In C# I came across Backgroundworker component for concurrency. Although it was slightly advanced than using a plain vanila thread, I found it to be much smoother.

I have not used SwingWorker in Java yet. While doing some coding on threads I came across this reference from JavaDoc for SwingWorker as a solution for threadsafety when working with swing objects.

Question: In C# I don't recall a statement like "not threadsafe for WinForm/WPF layouts, so use BackgroundWorker". I want to know if SwingWorker can be a substitute for threads in all aspect in Java, like how BackgroundWorker is used in C#?

For a novice, should I expect same performance/smooth running/full debug support for SwingWorker?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Hi ! This is unrelated to the post. Do you mind telling me if this is for a real application ? I had the impression that swing is dead and people only use it in legacy apps. – Erran Morad May 04 '14 at 01:39
  • 1
    @BoratSagdiyev: I must be dead. Forgive me. – Hovercraft Full Of Eels May 04 '14 at 01:41
  • @HovercraftFullOfEels - okay, I did not say that I swing is dead. I see almost zero jobs asking for swing as main skill. Also, I just heard it from most of the people I know. Do you mind telling me how I might be wrong ? – Erran Morad May 04 '14 at 01:44
  • I don't want to learn a dead feature, not do I want to waste time on it. So what do you recommend for [GUI instead of Swing](http://stackoverflow.com/questions/5828625/if-swing-is-deprecated-what-is-the-alternative) then? @BoratSagdiyev Are you referring to JSR for 7? – bonCodigo May 04 '14 at 01:53

1 Answers1

3

I want to know if SwingWorker can be a substitute for threads in all aspect in Java, like how BackgroundWorker is used in C#?

While a SwingWorker, which implements Runnable, Future and RunnableFuture, can be used anywhere a Runnable or Future is used, no, I wouldn't do this. A SwingWorker is a bit more complex to set up and use vs. a basic Thread/Runnable, and you would have no benefit to using it in non-Swing situations. The SwingWorker has been built to allow for creation of background threads while at the same time ensuring that certain portions of its code is called on the Swing event thread. When run in an application without a Swing event thread, this has no meaning.


Edit
You state:

That means a proper Swing based application has a combination of SwingWorker and basic Thread/Runnable for it to run smooth.

Where do you derive this from my statements above. Your statement doesn't make sense and is certainly not what I have stated above.

That's just useless and the reason I paid more attention - because it was directed by JavaDoc with the basis "Swing isn't threadsafe".

Which is also true of most all GUI libraries.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • That means a *proper* Swing based application has a combination of SwingWorker and basic Thread/Runnable for it to run *smooth*. That's just useless and the reason I paid more attention - because it was directed by JavaDoc with the basis "Swing isn't threadsafe"... – bonCodigo May 04 '14 at 01:45
  • I came back to Java, coz of J2EE. But do you think I made a mistake? and should have stuck with C#? – bonCodigo May 04 '14 at 01:59
  • 1
    @bonCodigo: This statement doesn't make sense. Both are tools, both have important uses, both are marketable. You can do well with either, it's not a one or the other situation, so please stop spouting nonsense and learn to use either or both tools and then move forward. – Hovercraft Full Of Eels May 04 '14 at 02:01
  • @HovercraftFullOfEels - Do you mind telling me why swing is still relevant today in the IT world ? Could there be any compelling reason to choose swing ? – Erran Morad May 04 '14 at 02:05
  • 2
    @BoratSagdiyev: no compelling reason. Again, they are all nothing but tools, and if they solve a problem, then they are useful. Swing is still useful for creating cross-platform desktop applications that are Java based. Period. Would I learn it purely for "marketability"? No. But can it solve some problems that I need solving? Yes -- so I use it. – Hovercraft Full Of Eels May 04 '14 at 02:13
  • At this point main motivation is for pure marketability as many jobs are asking for it. Plus it's worth *experiencing* if I am to make any relative comparisons in the future for technical-suitability front of a problem that needs to be solved. – bonCodigo May 04 '14 at 02:18