Re: problem in Threading !!!
Please surround your code with [highlight=Java] and [/highlight], it makes it much easier to read.
Here's what I think is happening:
Thread SINGO runs, gets to yield and lets BINGO run. BINGO gets to yield, and lets SINGO run. SINGO finishes, then BINGO finishes.
This results in the exact same output without the yield:
SINGO starts first, prints out thread info. BINGO starts, prints out thread info. SINGO prints out thread info and finishes. BINGO prints out thread info and finishes.
edit:
Here's a simple test class to get around this issue:
Code Java:
public class Test
{
public static class A implements Runnable
{
@Override
public void run()
{
System.out.println("Thread A");
Thread.yield();
System.out.println("Thread A");
}
}
public static class B implements Runnable
{
@Override
public void run()
{
System.out.println("Thread B");
System.out.println("Thread B");
}
}
public static void main(String[] args)
{
A a = new A();
B b = new B();
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}
There are some strange quirks with how the threads are executed, but with yield in there I get one of three possibilities: ABBA ("expected" result), AABB (thread A finishing before thread B even starts) or BBAA (strange quirk where thread B starts before thread A does? shouldn't ever happen...).
Without yield, I always get AABB
Re: problem in Threading !!!
If running this on a computer with more than a single processor (seems most these days have at least 2 processors), the multi-threading issue could get complicated. I don't know how the JVM divides up the work between multiple processors, or if yield works internally to the JVM or externally to the system, but the results above don't surprise me.
Re: problem in Threading !!!
See Postings on other forums for more discussions on this.
Re: problem in Threading !!!
You can never guarantee the order of how the threads will be run, therefore you will see the result BBAA. Even though Thread A is started before Thread B it doesn't mean the thread scheduler will run that thread first.
Never expect concurrent threads to run in a specific order.
// Json