0

In my program i am creating 2 tasks. These tasks implement the Callable interface. I am passing these to 2 threads to execute Thread pool class. My question is will these 2 threads start in same time? In java concurrency programming do all threads will start at same time?

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
rev
  • 77
  • 1
  • 8
  • 2
    This begs the question -- how can this be of any relevance? If you start a few threads in a loop, you can count on all of them running pretty soon after that. What guarantees do you exactly need? – Marko Topolnik Jul 02 '12 at 11:18

2 Answers2

3

in java concurrency programming all threads will start in same time?

No they won't. There is nothing you can do with conventional Java SE to force two threads to start at the same time. (Indeed, if you don't have a multi-core processor, it is physically impossible for two threads to start at the same time.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Only one thing to add: if the requirements are relaxed a bit than one can use http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html to make all the Threads wait for some condition before proceeding. – Rekin Jul 02 '12 at 11:18
  • Thanks stephen, rekin.. i wrote sample program and printing current time in run methods.Both threads are showing same time like : Running serviceProviderID=1 at 02/07/2012 04:57:51 Running serviceProviderID=2 at 02/07/2012 04:57:51 – rev Jul 02 '12 at 11:27
  • @rev it's only the same second, not necessarily the same time! – assylias Jul 02 '12 at 12:07
  • 1
    @Rekin Even if they wait on a condition, the only guarantee is that no thread will run **any sooner** than the condition is met. So we are at the square one. – Marko Topolnik Jul 02 '12 at 12:38
  • @MarkoTopolnik: Exactly, that what i meant by relaxing the requirements. – Rekin Jul 02 '12 at 12:48
  • @rev There are about 3 billion clock cycles in a second on a 3 GHz CPU. If they start at the same second, that means within 3 billion clock cycles. – Peter Lawrey Jul 02 '12 at 13:09
  • @Rekin The requirements would have to be relaxed by a very precise amount indeed if they are to accept your improvement, but not a simple `for (Thread t : threads) t.start();`. That was my point. – Marko Topolnik Jul 02 '12 at 13:33
  • How can display other threads result if one of threads consumes more time to execute a webservice? can i specify execution time of thread, if that thread not execeuted in specified time can i display other threds result in jsp? – rev Jul 02 '12 at 16:16
  • @rev - I don't really understand what you are asking, but the answer is probably that the JSP (or whatever) has to wait until all of the child threads have finished. One way to do this is to call `Thread.join(...)` on each of the child threads. – Stephen C Jul 03 '12 at 02:54
  • My question is thread-1 executing task-1 method and thread-2 executing task-2 method both threads are running inparallel. If thread-1 takes more time to process the request, i have cancelle that thread-1 then have to continue execution with thread-2. my situation thread-2 not starting the execution until thread-1 finishes. my sample code http://jsfiddle.net/hari034/cdUca/ – rev Jul 03 '12 at 03:13
0

If you want them to start at about the same time, there are several constructs that can help you.

You can use a CyclicBarrier - you create a new CyclicBarrier with 2 permits. Then each thread calls cbarrier.await() - neither thread will proceed until both have called await, which gives you pretty close to simultaneous execution, which is what I believe you want.

Also, In Java 7, there is a Phaser, which can do pretty much the same thing, but is supposed to be much more performant.

Victor Grazi
  • 15,563
  • 14
  • 61
  • 94