2009/11/2 Brent <[email protected]>
>
> I was curious... How many people have run into this bug that was
> introduced with JDK 5?
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6316090
>
> We've recently run into this issue and I was actually a little shocked
> to find out that the JVM behaved like this. Basically, if you spawn a
> bunch of threads at the same priority level, there is no guarantee
> that each thread will get CPU time which will cause thread
> starvation. Prior to JDK 5, the JVM placed all of these threads onto
> a queue so that each thread would get a slice of CPU in a FIFO manner,
> however since JDK 5 or greater this isn't the case.
>
> If you run the below test program you'll be able to demonstrate this
> issue. On my macbook pro, it appears that JDK 5 works ok, but JDK 6
> does not. This problem appears to be JDK + OS platform dependent.
> We've experienced this issue in production under heavy load with JDK 5
> on Solaris 10.
>
> To me... This seems like a serious flaw in the JVM and was wondering
> what others thought?
>
personally I wouldn't call it a flaw in the JVM... as that sunbug states:
"Java doesn't specify a particular scheduling order or fairness on
threads contending for a java-level monitor"
the issue is more that this code is relying on the behaviour of a particular
JVM implementation - this behaviour is not in the VM spec, so you could
have hit this moving between JVMs at any time.
the sunbug goes on to give several reasonable solutions, such as:
"Use one of the excellent "fair" lock constructs in java.util.concurrent"
which is imho the right thing to do - don't arbitrarily constrain the
runtime,
but provide methods to build "fairness" on top of the unordered primitives
just my 2 sen...
--
Cheers, Stuart
public class TestThreading {
>
> public static void main(String[] args) throws Exception {
> Thread threads[] = new Thread[100];
> int stats[] = new int[100];
>
> Thread stat_thread= new Thread(new Stat(stats));
> stat_thread.start();
>
> for (int i = 0; i < 100; i++) {
> threads[i] = new Thread(new Driver(i, stats));
> threads[i].start();
> System.out.println("starting thread " + i);
> }
>
> for (int i = 0; i < 100; i++) {
> threads[i].join();
> System.out.println("waiting for thread " + i);
> }
> stat_thread.interrupt();
> System.out.println("stat thread interrupted");
> System.out.println("Done!");
> }
>
> public static class Stat implements Runnable {
> private int[] stats;
>
> public Stat(int[] stats) {
> this.stats = stats;
> }
>
> public void run() {
> try{
> while (true) {
> Thread.sleep(20000);
> printTopThreads(100);
> }
> }
> catch(Exception e){
> e.printStackTrace();
> }
> }
>
> private void printTopThreads(int rank) {
> int[] temp = new int[100];
> System.arraycopy(stats, 0, temp, 0, 100);
>
> int max = 0;
> int idx = 0;
>
> for(int i=0; i<rank; i++) {
> max = 0;
> for(int j=99; j>=0; j--) {
> if(temp[j] >= max) {
> max = temp[j];
> idx = j;
> }
> }
> System.out.print("[");
> if(idx <10)
> System.out.print(" ");
> System.out.println( idx + "] - " + max);
> temp[idx] = -1;
> }
> System.out.println
> ("**********************************************");
> }
> }
>
>
> public static class Driver implements Runnable {
> private int id;
> private int[] stats;
>
> public Driver(int id, int[] stats) {
> this.id = id;
> this.stats = stats;
> }
>
> public void run() {
>
> BottleNeck b = BottleNeck.getInstance();
> for(int i = 0; i < 50; i++) {
> b.doSomething(id);
> stats[id] ++;
> try {
> Thread.sleep(25);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> }
>
> }
> }
>
> public static class BottleNeck {
> private static BottleNeck instance = new BottleNeck();
>
> public static BottleNeck getInstance() {
> return instance;
> }
>
> public synchronized void doSomething(int id) {
> try {
> System.out.println("Thread " + id);
> Thread.sleep(300);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> }
> }
>
> }
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---