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();
}
}
}
Related
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'
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.
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
I want to as if there is any way to execute a function after a specific time in windows phone 7.? For instance, see this code in android:
mRunnable=new Runnable()
{
#Override
public void run()
{
// some work done
}
now another function
public void otherfunction()
{
mHandler.postDelayed(mRunnable,15*1000);
}
Now the work done in upper code will be executed after 15 seconds of execution of otherfunction().
And I want to know is this possible in any way in windows phone 7 also.?
Thanx to all in advance..
Although you can use the Reactive Extensions if you want, there's really no need. You can do this with a Timer:
// at class scope
private System.Threading.Timer myTimer = null;
void SomeMethod()
{
// Creates a one-shot timer that will fire after 15 seconds.
// The last parameter (-1 milliseconds) means that the timer won't fire again.
// The Run method will be executed when the timer fires.
myTimer = new Timer(() =>
{
Run();
}, null, TimeSpan.FromSeconds(15), TimeSpan.FromMilliseconds(-1));
}
Note that the Run method is executed on a thread pool thread. If you need to modify the UI, you'll have to use the Dispatcher.
This method is preferred over creating a thread that does nothing but wait. A timer uses very few system resources. Only when the timer fires is a thread created. A sleeping thread, on the other hand, takes up considerably more system resources.
You can do that by using threads:
var thread = new Thread(() =>
{
Thread.Sleep(15 * 1000);
Run();
});
thread.Start();
This way, the Run method wil be executed 15 seconds later.
No need for creating threads. This can be done much more easier using Reactive Extensions (reference Microsoft.Phone.Reactive):
Observable.Timer(TimeSpan.FromSeconds(15)).Subscribe(_=>{
//code to be executed after two seconds
});
Beware that the code will not be executed on the UI thread so you may need to use the Dispatcher.
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) { }
}