It sounds like the event is going to be fired more than once... but you can't start the same thread more than once It sounds like your listener should implement the interface but start a thread directly in actionPerformed (or better, use an Executor so that it could use a thread pool). So instead of your current implementation, you could use: Assuming the listener implements runnable; you may want to // delegate that to a separate class for separation of concerns. Public void actionPerformed(ActionEvent e) { new Thread(this).start(); } or public void actionPerformed(ActionEvent e) { executor.
Execute(this); }.
It sounds like the event is going to be fired more than once... but you can't start the same thread more than once. It sounds like your listener should implement the interface but start a thread directly in actionPerformed (or better, use an Executor so that it could use a thread pool). So instead of your current implementation, you could use: // Assuming the listener implements runnable; you may want to // delegate that to a separate class for separation of concerns.
Public void actionPerformed(ActionEvent e) { new Thread(this).start(); } or public void actionPerformed(ActionEvent e) { executor. Execute(this); }.
This is exactly what I was missing. I was confusing my objects with the threads and no good was coming from it. I've since added an Executor and everything is running so much more smoothly now.
Thanks everyone who replied, you saved me hours of headache. – Mike May 29 at 23:38.
Okay, first thing: overcome the notion that your hundreds of threads will run in parallel. At the very best, they will run concurrently, ie, time-sliced. As you get into the hundreds of threads, you will see the bearings on the scheduling algorithm start to glow; in the thousands they'll smoke and eventually seize up, and you'll get no more threads.
Now, that said, we don't have near enough code to understand what you're really doing, but one thing that I note is you don't seem to be making new Threads. Remember that a thread is an object; the canonical way to start a thread is Thread t = new Thread(Runnable r); t.run(); What it looks like is that you're trying to run() the same thread over and over again; this way lies madness. Have a look at Wiki on Event Driven Programming.
If you really want to have a separate thread for handling each event, you'll want a scheme something like this (pseudocode): processEvents: function eventQueue: queue of Events event: implements Runnable -- something produces events and puts them on the queue loop -- forever do Event ev := eventQueue. Front new Thread(ev).run(); od end -- processEvents.
What I'd like is for each of the hundreds of instances to be its own thread, so that when an event is fired, they can all work in parallel. I don't think this is a good approach. Unless you have hundreds of processors, the threads cannot possibly all work in parallel.
You'll end up with the threads running them one at a time (one per processor), or time-slicing between processors. Each thread actually ties down a significant slice of the JVM's resources, even when inactive. IIRC, the default stack size is about 1 Mbyte.
The example code in your question shows the event calling start() on the thread. Unfortunately, you can only call start() on a thread once. Once the thread has terminated it cannot be restarted.
A better approach would be to create an executor with a bounded thread pool, and have each event cause a new task to be submitted to the executor. Something like this: ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue); ... public class IteratorStepListener implements StepEventListener, Runnable { public void actionPerformed(ActionEvent e) { executor. Submit(this); } public void run() { doStuff(); } }.
The solution allows the same instance to be executed by multiple threads concurrently. Morealso it could cause out-of-order event processing. It's not hundreds of instances to be its own thread, I know it might be obvious, just pointing – bestsss May 29 at 20:39.
You can't use threads like that in Java. This is because Java threads directly map to underlying OS threads (at least on JVM implementations that I'm aware of), and OS threads can't scale like that. A rule of thumb is, you want to keep total number of threads within hundred or something in an app.
A few hundred is probably ok. A few thousand gets usually problematic, depending on the HW you are using. The use of threads like you described is a valid implementation strategy in languages like Erlang for example.
Meanwhile, if you are stuck with Java this time, creating a shared thread pool and submitting your tasks to this instead of allowing all tasks to run concurrently might be a good alternative. In this case, you can choose a suitable number of threads (best number depends on the nature of the task. If you have no idea, number of CPU core available times 2 is a good start), and have that number of tasks run concurrently.
If you absolutely need all tasks to proceed concurrently, it could get a little complicated, but that's doable as well.
When a Java program starts, there is a single thread running – the main thread. Some of the navigation classes, e.g. Using the start method to start the thread. In leJOS NXJ 0.7 and later, threads can be created from any class that implements the Runnable interface.
When using threads, care should be taken with concurrency issues. Ensure that data is not read when it is in an inconsistent state. Methods and synchronized statements using a monitor object.
LeJOS implements a listener thread that listens for particular events. Button listeners are used to detect when a button is pressed, whatever your program is doing at the time. To listen for a press of a specific button, you register as listener for that button.
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.