I have 8 threads which each execute loops similar to this in their run method:
for (int i = 0; i < someVar; i++) {
for (int j = 0; j < someOtherVar; j++) {
if (Thread.currentThread().isInterrupted()) {
return;
}
// another loop and some calculations here
}
}
I start the Threads like this:
executor = Executors.newFixedThreadPool(threads);
// generate the threads and run them
for (int i = 0; i < threads; i++) {
MyClass cls = new MyClass(i);
executor.execute(cls);
}
Now I need to be able to pretty much instantly kill my threads, that's why I added the if statement above and use at another point executor.shutdownNow(). But it just isn't working fast enough. Some calculations might take a few seconds before they finish and the next iteration starts.
Is there any other reliable way to pretty much immediately shutdown threads?
You need an interrupt operation that calls into the thread and stops the current execution. Here's some documentation: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
Related
I am writing a program dealing with matrix parallel programming using Executorservice framework. And I set the fixedpoolsize to 4, however what surprises me is that when the matrix dimension is set to 5000, the speedup of using multithreading against serial execution is greater than 4 (which is also my CPU cores). And I have checked that my CPU does not support for hyperthreading.
Actually I use the Callable and Future container since my multithreading task requires the result to be returned.
// Part of code for parallel programming
double[][] x = new double[N][N];
List<Future<double[]>> futureList = new ArrayList<>();
for (int k=0;k<N;k++)
{
Future<double[]>temp=service.submit(new Thread.Task(N,k,matrix,vector));
futureList.add(temp);
}
for (int j = 0; j < N; j++) {
x[j]=futureList.get(j).get();
}
public double[] call() throws Exception {
for (int i = N - 1; i >= 0; i--)
{
double sum = 0;
for (int j = i + 1; j < N; j++)
{
sum += matrix[i][j] * x[j];
}
x[i] = (vector[i][k] - sum) / matrix[i][i];
}
return x;
}
// Part of code for Serial programming
double[][] x = new double[N][N];
for (int k=0;k<N;k++)
{
for (int i = N - 1; i >= 0; i--)
{
double sum = 0;
for (int j = i + 1; j < N; j++)
{
sum += matrix[i][j] * x[j][k];
}
x[i][k] = (vector[i][k] - sum) / matrix[i][i];
}
}
In short, I just take the inside loop away to let it be run by the thread and leave the outside loop unchanged.
But how can the speedup be like this?
Since from my previous concept it is that the maximum speedup can only be 4. And I have checked that the task is just done by 4 threads actually.
Threads can be utilized on the same cpu. You do not need a multi core processor to execute multi threaded applications.
Think of a thread as small process, that gets created by the parent program and destroyed once its done. Even single cpu computers can run multiple threads at once.
ExecutorService schedules threads to be executed and will run as many parallel threads as there are resources available including the cores.
Here is the docs on fixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed number of threads operating
off a shared unbounded queue. At any point, at most nThreads threads
will be active processing tasks. If additional tasks are submitted
when all threads are active, they will wait in the queue until a
thread is available. If any thread terminates due to a failure during
execution prior to shutdown, a new one will take its place if needed
to execute subsequent tasks. The threads in the pool will exist until
it is explicitly shutdown
You can also try workStealingPool
public static ExecutorService newWorkStealingPool()
Creates a
work-stealing thread pool using all available processors as its target
parallelism level.
This could be an effect of CPU cache affinity. If each core works on a different part of a problem it may achieve greater efficiency in cache use. Because RAM is up to 10's or more times slower than cache this can make a HUGE difference.
I miss thought about the stop condition of a loop, and therefore the program didn't stop raising CPU to its limits.
The whole Intellij process hangs and become totally unresponsive, knowing that my machine disposes i7 and I am running on Ubuntu which is normally multitasking as any modern OS. I imagine a solution like pausing a program if it continues executing without a change of state, after a time threshold or just when the memory state stagnates (many ideas can be thought).
Is there an established programming solution to stay safe from one of these situations (when testing, I am aware that an infinite program can be useful) but in case infinitely doing basic CPU operation, it is not.
Does running inside a differed thread help CPU charge dispatching resources ?
Is there a way in Intellij to stay safe from such a situation.
int rect_a = 100;
int rect_b = 50;
for (int j=1; j<rect_a; j++) {
for (int i=0; i<j; i++) {
//some computation
j = method() that returns 0 alwayse. (bad logic)
}
}
As your code is reset j to zero then the code will never end
consider
int rect_a = 100;
for (int j=1; j<rect_a; j++) {
for (int i=0; i<j; i++) {
j = 0;
}
}
System.out.println("ended");
ended is never printed. The computer is only doing what you tell it to
I want to launch a lot of tasks to run on a database of +-42Mio records. I want to run this in batches of 5000 records/time (results in 850 tasks).
I also want to limit the number of threads (to 16) java starts to do this for me and I am using the current code to accomplish this task:
ExecutorService executorService = Executors.newFixedThreadPool(16);
for (int j = 1; j < 900 + 1; j++) {
int start = (j - 1) * 5000;
int stop = (j) * 5000- 1;
FetcherRunner runner = new FetcherRunner(routes, start, stop);
executorService.submit(runner);
Thread t = new Thread(runner);
threadsList.add(t);
t.start();
}
Is this the correct way to do this? Particularly as I have the impression that java just fires away all tasks ...(FetcherRunner implements runnable)
The first part using ExecutorService looks good:
...
FetcherRunner runner = new FetcherRunner(routes, start, stop);
executorService.submit(runner);
The part with Thread should not be there, I am assuming you have it there just to show how you had it before?
Update:
Yes, you don't require the code after executorService.submit(runner), that is going to end up spawning a huge number of threads. If your objective is to wait for all submitted tasks to complete after the loop, then you can get a reference to Future when submitting tasks and wait on the Future, something like this:
ExecutorService executorService = Executors.newFixedThreadPool(16);
List<Future<Result>> futures = ..;
for (int j = 1; j < 900+ 1; j++) {
int start = (j - 1) * 5000;
int stop = (j) * 5000- 1;
FetcherRunner runner = new FetcherRunner(routes, start, stop);
futures.add(executorService.submit(runner));
}
for (Future<Result> future:futures){
future.get(); //Do something with the results..
}
Is this the correct way of working?
The first part is correct. But you shouldn't be creating and starting new Thread objects. When you submit the Runnable, the ExecutorService puts it on its queue, and then runs it when a worker thread becomes available.
.... I use the threadlist to detect when all my threads are finished so I can continue processing results.
Well if you do what you are currently doing, you are running each task twice. Worse still, the swarm of manually created threads will all try to run in parallel.
A simple way to make sure that all of the tasks have completed is to call awaitTermination(...) on the ExecutorService. (An orderly shutdown of the executor service will have the same effect ... if you don't intend to use it again.)
The other approach is to create a Future for each FetcherRunner's results, and attempt to get the result after all of the tasks have been submitted. That has the advantage that you can start processing early results before later ones have been produced. (However, if you don't need to ... or can't ... do that, using Futures won't achieve anything.)
You don't need to the part after the call to submit. The code you have that creates a Thread will result in 900 threads being created! Yowza. The ExecutorService has a pool of 16 threads and you can run 16 jobs at once. Any jobs submitted when all 16 threads are busy will be queued. From the docs:
Creates a thread pool that reuses a fixed number of threads operating
off a shared
unbounded queue. At any point, at most nThreads threads will be active processing tasks.
If additional tasks are submitted when all threads are active, they will wait in the
queue until a thread is available. If any thread terminates due to a failure during
execution prior to shutdown, a new one will take its place if needed to execute
subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
So there is no need for yet another thread. If you need to be notified after a task has finished you can have it call out. Other options are to cache all of the Future's returned from submit, and upon each task being finished you can check to see if all Future's are done. After all Future's are finished you can dispatch another function to run. But it will run ON one of the threads in the ExecutorService.
Changed from your code:
ExecutorService executorService = Executors.newFixedThreadPool(16);
for (int j = 1; j < 900 + 1; j++) {
int start = (j - 1) * 5000;
int stop = (j) * 5000 - 1;
FetcherRunner runner = new FetcherRunner(routes, start, stop);
executorService.submit(runner);
}
The best way would be to use countdownlatch as follows
ExecutorService executorService = Executors.newFixedThreadPool(16);
CountdownLatch latch = new CountdownLatch(900);
FetcherRunner runner = new FetcherRunner(routes, start, stop, latch);
latch.await();
in the FetcherRunner under finally block use latch.countDown(); code after await() will be executed only when all the tasks are completed.
I'm solving a multi-threaded problem in Java and have the core problem itself resolved, but my output isn't what I'd expect.
I have a main thread that spins new threads that each perform their tasks. It looks like the following (pseudocode):
initializeThreads(); // Creates the new threads
startThreads(); // Starts the new threads
sleep( duration ); // Lets the threads run for `duration` time
shutdownThreads(); // interrupt the threads
printOutput(); // output the results to the console
// definition of shutdownThreads() is at the end of my question
My issue happens upon trying to join() on each thread in my list of threads. Every now and then my program gets caught in an endless loop because I'm guessing the interrupt() I call on my list of threads doesn't happen fast enough compared to the join() and not all the threads get interrupted.
My other issue occurs when the program does shutdown the threads correctly and print the termination output. While the program is running each thread has messages that it logs to the console, and after I interrupt the threads and display the final program message some of these thread-specific logging messages leak out to the console.
For example, let's say one of my threads outputs "Thread-1 waiting for lock on object..." while it runs, and the main program finally wakes itself up, terminates the thread, and outputs "Program finished". Sometimes, the thread-specific message will show up after the program termination message.
If anyone could help me figure out how to stop this from happening please let me know!
shutdownThreads()
private void shutdownThreads() {
Thread t;
for (int i = 0; i < threads.size(); i++) {
t = threads.get(i);
t.interrupt();
}
for (int i = 0; i < threads.size(); i++) {
t = threads.get(i);
try {
t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted while waiting on thread to exit");
}
}
}
EDIT: One thing I thought of doing is rewriting shutdownThreads() to do this:
for (int i = 0; i < threads.size(); i++) {
Thread t = threads.get(i);
t.interrupt();
while (!t.isInterrupted()) ;
}
but this doesn't seem too elegant.
Your edited code at the bottom, that's gonna be a forever loop before it iterates the for() twice.
Here is a sample code if the thread size is 3 means its ok. where i can manage the memory. if the thread size is 50 there the problem lies. i need to set threadsize as 5. finished thread should be reuse the by other
Thread[] TCreate = new Thread[iThreadSize];
for (int i = 0; i< TCreate.length; i++) {
TCreate[i] = new Thread(new Runnable() {
public void run() {
lst.Add(this.getResult(url));
}
});
TCreate[i].setName("URL"+i);
TCreate[i].start(); }
for (int j = 0; j < TCreate.length; j++)
while (TCreate[j].isAlive())
Thread.sleep(10);
Can any one help what is use of setDaemon() method. what is purpose of Daemon
Please Help me.. Advance thanks
setDaemon controls whether a thread is a daemon thread or not. If daemon threads are still running when the program reaches the end of the main method, it does not keep the program from quitting. Non-daemon threads (user threads) do keep the program from quitting.
Java already includes methods for managing Thread pools.
calling Executors.newFixedThreadPool(5) will generate a thread pool with 5 worker threads for you.
Afterwards you can just assign Runnables that will be executed by on of the Threads in the pool.
See also:
How to implement simple threading with a fixed number of worker threads
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html