Re: what is wrong with File.createNewFile()
On Dec 1, 11:42 pm, Lew <l...@lewscanon.com> wrote:
// shouldn't this be while instead of if?
I don't think so. The 'wait()' will wait indefinitely here.
As soon as peer thread unlocks it will call 'notify()', and JVM
will elect one of the waiting threads. If it was 'notifyAll()',
then while is appropriate, because each time all the threads would
compete.
I made a test class. It seems that JVM has some round robin scheme for
waking threads, which suits me fine. Here is the example:
import java.util.*;
//
public class LockedThread extends Thread {
//
String name;
//
public LockedThread(String n)
{
super();
name=n;
}
//
public void run()
{
Mutex m=Mutex.getInstance();
Random r=new Random();
for(;;)
{
System.out.println(name+" at "+ new Date().getTime() + " trying
to lock");
m.lock();
System.out.println(name+" at "+ new Date().getTime() + "
locked");
try { sleep(1000 + r.nextInt(1000)); } catch(Exception ex) { }
m.unlock();
System.out.println(name+" at "+ new Date().getTime() + "
released");
try { sleep(1000 + r.nextInt(1000)); } catch(Exception ex) { }
}
}
//
public static void main(String[] argv)
{
int i;
LockedThread t;
for(i=0; i<10; i++)
{
t=new LockedThread("T"+i);
t.start();
try { sleep(100); } catch(Exception ex) { }
}
try { sleep(30000); } catch(Exception ex) { }
System.exit(0);
}
}
And here is the output, Tx is the thread identifier, big number is the
time, and the rest is status.
Note that Mutex is not in separate package this time. Note that the
order of output lines does not
reflect the order of execution sometimes, that's why the times are
included :
T0 at 1196610235532 trying to lock
T0 at 1196610235532 locked
T1 at 1196610235637 trying to lock
T2 at 1196610235741 trying to lock
T3 at 1196610235849 trying to lock
T4 at 1196610235953 trying to lock
T5 at 1196610236061 trying to lock
T6 at 1196610236169 trying to lock
T7 at 1196610236273 trying to lock
T8 at 1196610236377 trying to lock
T9 at 1196610236482 trying to lock
T0 at 1196610236821 released
T1 at 1196610236821 locked
T0 at 1196610237937 trying to lock
T1 at 1196610238393 released
T2 at 1196610238393 locked
T1 at 1196610239909 trying to lock
T2 at 1196610240369 released
T3 at 1196610240369 locked
T3 at 1196610241397 released
T4 at 1196610241397 locked
T2 at 1196610241933 trying to lock
T4 at 1196610242681 released
T5 at 1196610242681 locked
T3 at 1196610243205 trying to lock
T4 at 1196610243953 trying to lock
T5 at 1196610244469 released
T6 at 1196610244469 locked
T7 at 1196610245973 locked
T6 at 1196610245973 released
T5 at 1196610246401 trying to lock
T6 at 1196610247065 trying to lock
T7 at 1196610247897 released
T8 at 1196610247897 locked
T8 at 1196610248905 released
T9 at 1196610248905 locked
T7 at 1196610249369 trying to lock
T8 at 1196610249985 trying to lock
T9 at 1196610250005 released
T0 at 1196610250005 locked <----- check times here ****
T1 at 1196610251073 locked
T0 at 1196610251073 released
T9 at 1196610251769 trying to lock
T1 at 1196610252169 released
T2 at 1196610252169 locked
T0 at 1196610253017 trying to lock
T2 at 1196610253565 released
T3 at 1196610253565 locked
T1 at 1196610254137 trying to lock
T3 at 1196610254729 released
T4 at 1196610254729 locked
T2 at 1196610255013 trying to lock
T3 at 1196610255789 trying to lock
T4 at 1196610256289 released
T5 at 1196610256289 locked
T4 at 1196610257557 trying to lock
T5 at 1196610257829 released
T6 at 1196610257829 locked
T6 at 1196610259281 released
T7 at 1196610259281 locked
T5 at 1196610259769 trying to lock
T6 at 1196610260369 trying to lock
T7 at 1196610260665 released
T8 at 1196610260665 locked
T7 at 1196610261777 trying to lock
T8 at 1196610262197 released
T9 at 1196610262197 locked
T8 at 1196610263933 trying to lock
T9 at 1196610264201 released
T0 at 1196610264201 locked
T9 at 1196610265725 trying to lock
T0 at 1196610266137 released
T1 at 1196610266137 locked
DG