Hello,

yes. synchronized doesn't use fair locking.

Hum. I have a doubt about "Stat" thread. The run method doesn't use
any kind of synchronization to read shared "stats" array. Is there a
guarantee that the values it prints out is not stale? Me. I wouldn't
say that.


Well. On my Quad Core linux your test looks just fine.
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)

[ 2] - 46
[ 3] - 46
....
[10] - 46
[11] - 45
[12] - 45
...
[99] - 45
[ 0] - 23
[ 1] - 23

Better then we could even expect.

alexander
On Nov 1, 6:59 pm, Brent <[email protected]> wrote:
> 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?
>
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to