0

Well, I'm trying to understand this case. When i create two thread sharing the same instance of Runnable. Why is this order?

Hello from Thread t 0 Hello from Thread u 1 Hello from Thread t 2 Hello from Thread t 4 Hello from Thread u 3 <----| this is not in order Hello from Thread u 6 Hello from Thread t 5 <----| this one too Hello from Thread t 8 Hello from Thread t 9 Hello from Thread t 10 

i'll show you the code of two thread:

public class MyThreads { public static void main(String[] args) { HelloRunnerShared r = new HelloRunnerShared(); Thread t = new Thread(r,"Thread t"); Thread u = new Thread(r,"Thread u"); t.start(); u.start(); } } 

And concluding, the final question is if i'm running this thread i understand they're not running in order but. Why a thread is keeping or printing a number in disorder?

This is the code for the runnable:

class HelloRunnerShared implements Runnable{ int i=0; public void run(){ String name = Thread.currentThread().getName(); while (i< 300) { System.out.println("Hello from " + name + " " + i++); } } } 

i thought they would be processed intercalated. It's just an assumption!!

Thanks!

4
  • 3
    Post the code the threads are running? (HelloRunnerShared) Commented Aug 14, 2015 at 4:09
  • 1
    It all depends on which thread gets CPU first. The order may vary with each run of program. If you want to maintain order, use producer/ consumer with wait/notify or use advanced concurrent API ExecutorService Commented Aug 14, 2015 at 4:16
  • post the code of HelloRunnerShared class Commented Aug 14, 2015 at 4:20
  • @sunrise76 yes you're right, i'm understanding it, i guess! i thought i should be intercalated. Commented Aug 14, 2015 at 16:52

2 Answers 2

1

Why do you think threads should be executing in a particular order? It's a nondeterministic phenomenon -- whichever is scheduled first, runs first.

Use the ExecutorService.invokeAll if you want things to run in the order in a fixed order, regardless of their schedule.

Sign up to request clarification or add additional context in comments.

4 Comments

thats why they invented (sleep()) for getting the data in particular. order
Calling sleep() is a generally accepted bad practice for many reasons, often used as crutch for a poor design. Without listing things myself, I'll direct you to this article: javamex.com/tutorials/threads/sleep.shtml
@AnandDwivedi, sleep() never fixes anything. If sleep() makes a bug seem to go away during testing, it's only because you've made the bug less likely to occur. It still can occur, and if and when it finally does occur it will be at a customer site instead of in your test rig.
If i want something to be executed in a fixed order I should think about other solutions than threads.
1

There are several things going on:

  • The OS scheduler can switch between threads any time it wants. There's no fairness requirement, the scheduler may favor one thread over another (for instance, it could be trying to minimize the amount of context-switching).

  • The only locking going on is on the PrintStream used by the println method, which keeps the threads from writing to the console simultaneously. Which thread acquires the lock on the PrintStream when depends on the OS scheduler. The locks used are the intrinsic ones used with the synchronized keyword, they are not fair. The scheduler can give the lock to the same thread that took it last time.

  • ++ is not an atomic operation. The two threads can get in each other's way updating i. You could use AtomicInteger instead of an int.

  • Access to i is not protected by a lock or any other means of enforcing a happens-before boundary, so updates to it may or may not be visible to other threads. Just because one thread updates i doesn't automatically mean the other thread will see the updated value right away, or at all (how forgiving the JVM is about this depends on the implementation). In the absence of happens-before boundaries the JVM can make optimizations like reordering bytecodes or performing aggressive caching.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.