Hi,

Possible Bug in natives threads, some thread that are waiting, never
wake up with notify ()

0. run 100 threads each of them  has a  lock <BusyFlag>
1.They start,
2.they are locking their  busyFlag  <busyFlag.getBusyFlag();>
3.waiting for each other to a <Barrier>
4. continue and unlock their busyFlag <busyFlag.freeBusyFlag();>
5. for All the other threads
        5.1. try to lock all the busyFlag , wait if busy else when it's
done
        5.2 do somthing that use the same synchronized methode (or a
static one like System.out.println())
        5.3 release the busyFlag (notify()) (notifyAll() doesn't solve
the problem)
6. end for all


Some thread that wait at state 5.1,  never wake up with the notify() at
5.3
=> It works with green threads
=> the <BusyFlag>  an <Barrier> object comes from the book "Java
threads" 2nd edition(Java 2) from Scott Oasks & Henry Wong (ed O'Reilly)

    This is not a proof that they are free of bugs but they really seems
to bo ok.
=> Sometime I got the same probleme with huge concurrent acces to the
synchronized methode (like System.out.println()) some thread got the
lock some other are
for the end of the synchronized method and never wake up.


Try the code more than once (5 or 6 time) sometime it works... I've
tried it on two linux box (bi pentiumII kernel 2.2.9) (celeron kenel
2.2.13) and I got the same trouble.

Please help....the SMP box was bought to use the kernel level
threads.....

/*
*********************************************************************************
*/
import java.io.*;
import java.util.*;

public class Main{

    public static void main(String argv[])
    {
 // number of threads
 int nbThread = 50;
 // meeting point
 Barrier barrier = new Barrier(nbThread);

 // a object with a synchronized methode
 VectorOfString vectorOfString = new VectorOfString();

 // tab of threads
 MyThread [] tabThread = new MyThread[nbThread];

 //creation of the object
 for(int i=0; i < nbThread; i++)
     tabThread[i] = new MyThread(tabThread, nbThread, barrier,
vectorOfString);

 // start all threads (that will wait each other will executing the )
 for(int i=0; i < nbThread; i++)
     tabThread[i].start();
    }
}


class MyThread implements Runnable {
    MyThread []    tabThread; int nbThread;
    Thread         activite;
    Barrier        barrier;
    VectorOfString vectorOfString;
    BusyFlag    busyFlag;


    public MyThread(MyThread [] _tabThread,  int _nbThread, Barrier
_barrier, VectorOfString _vectorOfString ){
 tabThread = _tabThread; nbThread  = _nbThread;
 barrier = _barrier;
 vectorOfString = _vectorOfString;
 busyFlag  = new BusyFlag();

    }
    public void start ()
    {
 activite = new Thread (this);
 activite.start ();
    }
    public void run()
    {
 // lock of the local flag
 busyFlag.getBusyFlag();
 //System.out.println(Thread.currentThread().getName()+" before rendez
Vous");
 // go to the barrier
 try{barrier.waitForRest();}catch (InterruptedException ex)
{System.out.println("toto");}
 //System.out.println(Thread.currentThread().getName()+" after rendez
Vous");

 // free the local flag
 busyFlag.freeBusyFlag();

 // for all other thread try to lock the flag do something then release
the flag
 for(int i=0; i < nbThread; i++){
     MyThread watchedThread = tabThread[i];

     watchedThread.busyFlag.getBusyFlag();

     // try with this:

vectorOfString.addString(Thread.currentThread().getName()+Thread.currentThread().getName());

     vectorOfString.addString((new Integer(i)).toString());

     // or with this:

//System.out.println(Thread.currentThread().getName()+Thread.currentThread().getName());

     watchedThread.busyFlag.freeBusyFlag();
 }
    }
}

class Barrier{
    private int threads2Wait4;
    private InterruptedException iex;

    public Barrier (int nThreads){
 threads2Wait4 = nThreads;
    }
    public synchronized int waitForRest()
 throws InterruptedException{
 int threadNum = --threads2Wait4;

 if (iex != null) throw iex;
 if (threads2Wait4 <= 0){
     notifyAll();
     return threadNum;
 }
 while (threads2Wait4 > 0){
     if (iex != null) throw iex;
     try {
  wait();
     }catch (InterruptedException ex) {
  iex = ex;
  notifyAll();
     }
 }
 return threadNum;
    }

    public synchronized void freeAll() {
 iex = new InterruptedException("Barrier Released by freeAll");
 notifyAll();
    }
}
class BusyFlag{
    protected Thread busyflag  = null;
    protected int    busycount = 0;

    public  synchronized void getBusyFlag(){
 while(tryGetBusyFlag() == false){
     try{
  wait();
     } catch(Exception e){System.err.println("Exception on wait");}
 }
    }
    public synchronized boolean tryGetBusyFlag(){
 if(busyflag == null){
     busyflag = Thread.currentThread();
     busycount = 1;
     return true;
 }
 if(busyflag == Thread.currentThread()){
     busycount++;
     return true;
 }
 return false;
    }
    public synchronized void freeBusyFlag(){
 if(getBusyFlagOwner() ==  Thread.currentThread()){
     busycount--;
     if(busycount == 0){
  busyflag = null;
  notify();
     }
 }
    }
    public synchronized Thread getBusyFlagOwner(){
 return busyflag;
    }
}

class VectorOfString{
    Vector messages = new Vector();
    public synchronized void addString(String str)
    {
 messages.addElement(str);
    }
}

/*
*********************************************************************************
*/

Best regards,

Edouard Duchesnay

[EMAIL PROTECTED]




----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to