Java GUI and Multiple Instances of the SwingWorker Class?

Yes, the more processing you do, naturally, it will affect the performance of the computer. After all, processing resources is a limited resource.

Yes, the more processing you do, naturally, it will affect the performance of the computer. After all, processing resources is a limited resource. That said, many modern computers have multiple cores, which means that a single-threaded application will probably not be able to take full advantage of the processor resources.In general, having a few threads running is not going to be a big problem.

However, once there are hundreds or thousands of threads, performance can degrade, as the time it takes to do a context switch between threads can start to take up a larger fraction of the processing resources that are available.

It depends on the number of cores you have. For instance, if you have two cores you can safely spawn one background task because you spawn two threads, one for the UI and one for the task, which will nicely split among the two cores. However, the UI task is often idle (waiting for the user to do something).

Thus in 99% of the cases, on a two core machine you can spawn two worker threads (in addition to the standard UI thread that is always there).

If you delve into the SwingWorker code you'll see the following constant defined: /** * number of worker threads. */ private static final int MAX_WORKER_THREADS = 10; Hence, the number of background threads can never exceed this value irrespective of the number of SwingWorker's you actually create. One way to vary the background threading model would be to plug your own ExecutorService into the AppContext associated with the SwingWorker class.

However, this is slightly dodgy given that AppContext belongs to sun. Awt and hence is not part of the official JDK API. // Create single thread executor to force all background tasks to run on the same thread.

ExecutorService execService = Executors. NewSingleThreadExecutor(); // Retrieve the AppContext. *CAUTION*: This is part of the sun.

Awt package. AppContext ctxt = AppContext.getAppContext(); // Verify that nothing is already associated with SwingWorker. Class within the context.

Object obj = ctxt. Get(SwingWorker. Class); if (obj!

= null) { throw new IllegalStateException("Object already associated with SwingWorker: " + obj); } // Install ExecutorService. Will be retrieved by the SwingWorker when execute() is called. Ctxt.

Put(SwingWorker. Class, ctxt).

Aha. That's good to know. I didn't notice that.

– RCC Jan 5 '10 at 15:32 1 It's also worth noting that since SwingWorker is a Runnable, you can instead run SwingWorkers through your own ExecutorService if you require more control over their threading. – Sbodd Jan 5 '10 at 15:36.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions