ThreadPoolExecutor is not executing concurrently? - multithreading

This is for my academic purpose only. Are the tasks that we add to the executor service are really executing in parallel. Well this is my example that raised this question
Runnable Class
public Tasks implement Runnable{
int taskCount;
public Tasks(int count){
this.taskCount = count;
}
public void run(){
System.out.println("In Task :"+taskcount +" run method");
}
}
Main Class
Class MyTest {
public static void main(String args[]){
ExecutorService service = Executors.newFixedThreadPool(10);
for(inti=0;i<10;i++){
Tasks taskObj = new Tasks(i);
service.submit(taskObj);
}
service.shutdown();
}
}
As soon as i submit a taskObj to the executor, the taskObj run() is invoked.
What if i have to something like this,
Add all the taskObj to the executor , the run() must not get invoked
Execute all the task objects at one shot. All the taskobj run() must be executed in parallel/concurrently
Please let me know
Thanks...V

If I understood you right, one way to solve this would be to use thread barriers. This might sound strange, but is actually implemented quite easy. You just take a variable (lets name it traffic-light) and make every thread loop on it. If you started enough threads (starting a new thread might consume some time) you just change it to green and all your threads will start execution at the same time.
For academic purposes we used to take an atomic-integer as counter (initialized with 0) and started n threads. The task of each threads was to increase the counter and then loop on it until it reached n. Like this you'll have all threads as parallel as possible.
If you still want to go with a thread pool system, you might have to implement your own thread system, where threads can wait upon a signal prior to grabbing work.
good luck

Related

How to hit 1000 endpoints using multi threading in groovy?

I have a requirement to hit endpoints more than 1000 times to fetch some data from website. So i read some tutorials to use Multi Threading to achieving it. But at a time i want to use only 13 threads on same method.
So basically i am using ExecutorService to run 13 threads at one time:
ExecutorService threadPool = Executors.newFixedThreadPool(13);
for (int itLocation = 0; itLocation < locationList.size(); itLocation++) {
//some code like
ScraperService obj = new ScraperService(threadName,url)
threadPool.submit(obj);
}
threadPool.shutdown();
My Groovy Class named as ScraperService is implementing the Runnable interface.
#Override
void run() {
println("In run method...................")
try {
Thread.sleep(5000);
someMethod()
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Problem:
My problem is that my ExecutorService.submit(obj) and ExecutorService.execute(obj) is not calling my run() method of Runnable interface.
In Groovy/Grails:
There is also a executor plugin Executor Plugin in grails but i didn't found any appropriate example how to use it.
GPars is excellent for this type of thing.
Your ScraperService can just be responsible for handling the scraped data like below or maybe fetching it too, whatever.
import groovyx.gpars.GParsPool
def theEndpoint = 'http://www.bbc.co.uk'
def scraperService
GParsPool.withPool( 13 ) {
(1..1000).eachParallel {
scraperService.scrape theEndpoint.toURL().text
}
}
First of all i think there is problem in groovy service class with #Transnational annotation which doesn't allow to call run() method of Runnable interface. If you'll remove the #Transnational then it will call the run() method. It happened also in my case. But i am not sure, there may be some another reason. You can directly use:
ExecutorService threadPool = Executors.newFixedThreadPool(13)
threadPool.execute(new Runnable() {
#Override
void run() {
Thread.sleep(5000);
someMethod()
}
})
Extra (As i read your question)
If you are using multiple threads on same method then it can be complex as all threads will use the same local variables of that method which can occurs problems. It is better to use multiple threads for different different work.
But if you want to use same method for executing multiple threads then in my scenario it is better to use Executors.newSingleThreadExecutor().
ExecutorService threadPool = Executors.newSingleThreadExecutor();
newSingleThreadExecutor() calls a single thread so if you want to execute multiple tasks on it then it does not create multiple threads. Instead, it waits for one task to complete before starting the next task on the same thread.
Speed: newSingleThreadExecutor in comparison of multi threads will be slow but safer to use.
the threadPool.submit does not execute task
use threadPool.execute(obj) or threadPool.submit(obj).get()
instead of threadPool.submit(obj)
check the documentation for details:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#submit(java.util.concurrent.Callable)
example:
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
ExecutorService pool=Executors.newFixedThreadPool(3)
for(int i=0;i<7;i++){
int x=i;
Thread.sleep(444);
pool.execute{
println "start $x"
Thread.sleep(3000+x*100);
println "end $x"
}
}
println "done cycle"
pool.shutdown() //all tasks submitted
while (!pool.isTerminated()){} //waitfor termination
println 'Finished all threads'

What is the purpose of await() in CountDownLatch?

I have the following program, where I am using java.util.concurrent.CountDownLatch and without using await() method it's working fine.
I am new to concurrency and want to know the purpose of await(). In CyclicBarrier I can understand why await() is needed, but why in CountDownLatch?
Class CountDownLatchSimple:
public static void main(String args[]) {
CountDownLatch latch = new CountDownLatch(3);
Thread one = new Thread(new Runner(latch),"one");
Thread two = new Thread(new Runner(latch), "two");
Thread three = new Thread(new Runner(latch), "three");
// Starting all the threads
one.start(); two.start(); three.start();
}
Class Runner implements Runnable:
CountDownLatch latch;
public Runner(CountDownLatch latch) {
this.latch = latch;
}
#Override
public void run() {
System.out.println(Thread.currentThread().getName()+" is Waiting.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println(Thread.currentThread().getName()+" is Completed.");
}
OUTPUT
two is Waiting.
three is Waiting.
one is Waiting.
one is Completed.
two is Completed.
three is Completed.
CountDownLatch is the synchronization primitive which is used to wait for all threads completing some action.
Each of the thread is supposed to mark the work done by calling countDown() method. The one who waits for the action to be completed should call await() method. This will wait indefinitely until all threads mark the work as processed, by calling the countDown(). The main thread can then continue by processing the worker's results for example.
So in your example it would make sense to call await() at the end of main() method:
latch.await();
Note: there are many other use cases of course, they don't need to be threads but whatever that runs usually asynchronously, the same latch can be decremented several times by the same task etc. The above describes just one common use case for CountDownLatch.

WCF InstanceContextMode.PerCall Services and Multi-Threading

I am running a self-hosted WCF Service (it will be a Windows Service but I'm runnning it as a Console App for debugging). The server side code needs to do some work when it is called. Then pass back the result synchronously to the client. It then needs to carry on and do some more intensive long-running processing.(ie asynchronously)
A simplified illustration of the code is :
[ServiceBehavior(IncludeExceptionDetailInFaults = true,InstanceContextMode = InstanceContextMode.PerCall)]
public class MyWCFService : IMyWCFService, IDisposable {
private Thread importThread;
private DoWork importWork;
public MyImportResultContract StartImport(MyImportRequestContract requestParams) {
processorResult = new MyImportResultContract();
//Simulate a short piece of work here to return results to the
//caller before calling a Thread to do more processing
processorResult.Success = true;
processorResult.Messages = "A message to pass back to caller"
Console.WriteLine("WCF Class ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);
//Thread off to do long running process
importWork = new DoWork();
importThread = new Thread(importWork.Start);
importThread.Start();
//Pass back the results
return processorResult;
}
public void Dispose() {
Console.WriteLine("WCF Class Dispose : " + DateTime.Now.ToLongTimeString());
}
}
internal class DoWork : IDisposable {
public void Start() {
Console.WriteLine("Thread ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);
//Simulate a long running process
Thread.Sleep(60000);
Console.WriteLine("Thread Finished");
}
public void Dispose() {
Console.WriteLine("DoWork Class Dispose : " + DateTime.Now.ToLongTimeString());
}
}
I have 2 questions:
1. I'm seeing the Dispose of the Service Class called before the Thread it spawns carries on and completes. My concern is that the Service class instance might not be getting released for Garbage collection because of the thread it spawns. Is the code outlined here solid so that this won't happen?
2. The service is self hosted in a Windows Service. What will happen if the service is stopped to any spawned threads ? Is there a way to get the service to wait until any Threads still running complete ?
Of course the service class can be disposed before the thread completes. If you wanted to wait for the work in the thread to complete, why start a thread? I'm not sure what behavior you expect/want other than this.
Yes, if the process is shut down your threads die. You have to implement a handler for the service stop callback which delays the shutdown process until all workers are complete. This will be easier to do if you use the more modern Task instead of Thread. Probably, you need to keep a global list of tasks in process so that your shutdown code can WaitAll on those tasks.

Windows Azure Worker Role, what to do with the main thread?

So we're setting up a worker role with windows azure and it's running at a very high cpu utilization. I think it has something to do with this section, but I'm not sure what to do about it. Each individual thread that gets started has its own sleep, but the main thread just runs in a while loop. Shouldn't there be a sleep in there or something?
public class WorkerRole : RoleEntryPoint
{
private List<ProcessBase> backgroundProcesses = new List<ProcessBase>();
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning);
}
}
while (true) { }
}
How about something like this, would this be appropriate?
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
List<Task> TaskList = new List<Task>();
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
TaskList.Add(Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning));
}
}
Task.WaitAll(TaskList.ToArray());
//while (true) { }
}
Your change looks good to me. Sometimes I use Thread.Sleep(Timeout.Infinite).
Have you tested it? Does it reduce the CPU usage? It could be that the tasks themselves actually consume a lot of CPU. We don't know for sure yet that the while loop is the culprit.
The while loop is probably causing your high CPU. It's basically an infinite busy-wait. Your second code sample should work fine, as long as the Tasks you're waiting on never exit. In my personal opinion the best solution is the one outlined in my answer here. If you don't like that, a simpler solution would be to add a Sleep() inside the loop. eg:
while(true){
Thread.Sleep(10000);
}
while loop with empty body will fully load the CPU core to which the thread is dispatched. That's bad idea - you burn CPU time for no good.
A better solution is to insert a Thread.Sleep() with a period ranging anywhere from 100 milliseconds to infinity - it won't matter much.
while( true ) {
Thread.Sleep( /*anything > 100 */ );
}
Once you've got rid of the empty loop body you're unlikely to do any better than that - whatever you do in your loop the thread will be terminated anyway when the instance is stopped.
Just deployed this to production this morning after testing it last night on staging. Seems to be working great. CPU usage went down to .03% average for the background process down from 99.5% ...
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
List<Task> TaskList = new List<Task>();
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
TaskList.Add(Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning));
}
}
Task.WaitAll(TaskList.ToArray());
//while (true) { }
}

Fire off multiple synchronous threads

I'm not sure if this is a silly question as I don't know much about threads, but is it possible to fire off multiple synchronous threads at the same time, and wait for all to complete before acting? If it is how do you do it?
Certainly the simplest way is to use .NET 4.0 's Task Parallel Library (TPL).
e.g.
Parallel.For(0, 10, x =>
// Do this in Parallel.
System.Diagnostics.Debug.WriteLine(x)
);
see: http://msdn.microsoft.com/en-us/concurrency/bb964701
ut is it possible to fire off multiple synchronous threads at the same time, and wait for all to complete before acting?
"synchronous threads" is an oxymoron, they don't exist.
Of course you can start multiple threads and wait for them to complete (Thread.Join(otherThread))
If it is how do you do it?
Very rarely. Always use as few threads as possible. They are expensive.
Make sure you know about the ThreadPool and (Fx4) the Tasks library, TPL
You can use Parallel.Invoke.
This will execute the supplied actions in parallel and return when all are finished.
You can't really do anything at the same time, let alone fire threads :) (you can fire them rapidly one after the other though, although it is possible that a thread will start before the last one is fired).
As for waiting for them all before continuing, you can use the Join method, which waits for a thread to end before continuing.
Generally you'd do with the construct like below,
public class MultipleThreqadTest
{
private readonly Thread[] threads;
private readonly object locker;
private int finishCounter;
private readonly AutoResetEvent waitEvent;
public MultipleThreqadTest()
{
threads=new Thread[10];
for(int i=0;i<0;i++)
threads[i]=new Thread(DoWork);
finishCounter = threads.Length;
waitEvent=new AutoResetEvent(false);
}
public void StartAll()
{
foreach (var thread in threads)
{
thread.Start();
}
//now wait for all worker threads to complete
waitEvent.WaitOne();
}
private void DoWork()
{
//Do Some Actual work here, you may need to lock this in case you are workin on some shared resource
//lock(locker)
//{
//}
//Check if all worker thread complets
if(Interlocked.Decrement(ref finishCounter)==0)
{
this.waitEvent.Set();
}
}
}

Resources