Re: synchronize keyword may produce deadlocks.

2001-05-28 Thread Paul Speed

You do know your code has a typo that will cause deadlocks, don't
you?  See below...

[EMAIL PROTECTED] wrote:
 
 Hi,
The synchronize call may deadlock due to thread starvation. This is present
 in all versions of java on all platforms i could test (jdk 1.1 thru 1.4b
 on win/sol/lin)
 Example code for interlock.java is as follows :
 import java.io.*;
 import java.lang.*;
 public class interlock{
 public static int test=0; // 0 - test java synchronize. 1 - test java regular
 private static boolean atomic_spinlock1=false;
 private static boolean atomic_spinlock2=false; public interlock(){}
 public static void main(String[] args) throws InterruptedException {
 log(InterlockTest : If this code completes test was a success.); 
if(args.length==1){test=Integer.valueOf(args[0]).intValue();}
 new interlock().startup();}
 public static void log(String s){ System.err.println(+s); }
 public static void blink(int 
i){try{Thread.currentThread().sleep(10+i);}catch(Exception
 e){}}
 public static void spinlock1_rel(){atomic_spinlock1=false;}
 public static boolean spinlock1_set(){
 if (atomic_spinlock1==false) { atomic_spinlock1=true; return true; } else
 { return false; } }
 public static void spinlock2_rel(){atomic_spinlock2=false;}
 public static boolean spinlock2_set(){
 if (atomic_spinlock2==false) { atomic_spinlock2=true; return true; } else
 { return false; } }
 public static synchronized void s_spinlock1_rel(){atomic_spinlock1=false;}
 public static synchronized boolean s_spinlock1_set(){
 if (atomic_spinlock1==false) { atomic_spinlock1=true; return true; } else
 { return false; } }
 public static synchronized void s_spinlock2_rel(){atomic_spinlock2=false;}
 public static synchronized boolean s_spinlock2_set(){
 if (atomic_spinlock2==false) { atomic_spinlock2=true; return true; } else
 { return false; } }
 private void startup() { if (test==0) {log(Testing atomic spinlocks with
 Java Virtual Machine sync ); }
 else {log( Testing atomic spinlocks without Java Virtual Machine sync...);}
 Runner y=new Runner(0);Runner s=new Runner(1); y.start();blink(100);s.start();
 }
 class Runner extends Thread implements Runnable{ private int runr=0;
 Runner(int seq) { runr=seq; } public void run() { log( Started runner +runr+
 -- );
 log(runr+: Setting atomic spinlock 1...);
 if (test!=0){while(spinlock1_set()==false){}} else { 
while(s_spinlock2_set()==false){}

Note in the line above that test!=0 is setting spinlock 1 and the else
branch is setting spinlock 2.  So, whenever you test the synchronized
version you are going to get a deadlock since your locking has no
thread identity... a thread can block itself by checking the lock
after setting it.

-Paul Speed

 }
 log(runr+: Set spinlock 1. Now sleeping... );
 blink(1000); log(runr+: Finished sleeping. Now setting spinlock 2... (if
 bug exists it will hang here...) );
 if (test!=0){while(spinlock2_set()==false){}} else { while(spinlock2_set()==false){}
 }
 log(runr+: Spinlock 2 set. Now unsetting both spinlocks... );
 if (test!=0){spinlock2_rel(); spinlock1_rel();}else{s_spinlock2_rel(); 
s_spinlock1_rel();}
 log(runr+: Completed. );  }}  }
 
 BTW, my email has changed.
 [EMAIL PROTECTED] -- [EMAIL PROTECTED]
 Thanks.
 -Ys-
 
 Free, encrypted, secure Web-based email at www.hushmail.com



Re: synchronize keyword may produce deadlocks.

2001-05-28 Thread yhs1634



You do know your code has a typo that will cause deadlocks, don't
you?  See below...
SNIP

arrgh. i screwed that one up. correcting the typo allows the code
to run fine. ok..i cant prove it with my example code, but under 
heavy loads the deadlock does occurunfortunately im not yet
ready to distribute the code that did produce the deadlock under
heavy loads as open source. :(
i'd recommend against synchronize based on the behaviour i saw
but i cant prove it with example code right now. Dekker's algorithm
with a Thread.sleep(10) at the end seems to be the cure.
-Ys-
Free, encrypted, secure Web-based email at www.hushmail.com


synchronize keyword may produce deadlocks.

2001-05-27 Thread yhs1634

Hi,
   The synchronize call may deadlock due to thread starvation. This is present
in all versions of java on all platforms i could test (jdk 1.1 thru 1.4b 
on win/sol/lin)
Example code for interlock.java is as follows :
import java.io.*;
import java.lang.*;
public class interlock{
public static int test=0; // 0 - test java synchronize. 1 - test java regular
private static boolean atomic_spinlock1=false;
private static boolean atomic_spinlock2=false; public interlock(){}
public static void main(String[] args) throws InterruptedException {
log(InterlockTest : If this code completes test was a success.); 
if(args.length==1){test=Integer.valueOf(args[0]).intValue();} 
new interlock().startup();} 
public static void log(String s){ System.err.println(+s); }
public static void blink(int 
i){try{Thread.currentThread().sleep(10+i);}catch(Exception 
e){}}
public static void spinlock1_rel(){atomic_spinlock1=false;}
public static boolean spinlock1_set(){ 
if (atomic_spinlock1==false) { atomic_spinlock1=true; return true; } else 
{ return false; } }
public static void spinlock2_rel(){atomic_spinlock2=false;}
public static boolean spinlock2_set(){ 
if (atomic_spinlock2==false) { atomic_spinlock2=true; return true; } else 
{ return false; } }
public static synchronized void s_spinlock1_rel(){atomic_spinlock1=false;}
public static synchronized boolean s_spinlock1_set(){ 
if (atomic_spinlock1==false) { atomic_spinlock1=true; return true; } else 
{ return false; } }
public static synchronized void s_spinlock2_rel(){atomic_spinlock2=false;}
public static synchronized boolean s_spinlock2_set(){ 
if (atomic_spinlock2==false) { atomic_spinlock2=true; return true; } else 
{ return false; } }
private void startup() { if (test==0) {log(Testing atomic spinlocks with 
Java Virtual Machine sync ); } 
else {log( Testing atomic spinlocks without Java Virtual Machine sync...);}
Runner y=new Runner(0);Runner s=new Runner(1); y.start();blink(100);s.start(); 
}
class Runner extends Thread implements Runnable{ private int runr=0;
Runner(int seq) { runr=seq; } public void run() { log( Started runner +runr+ 
-- );
log(runr+: Setting atomic spinlock 1...); 
if (test!=0){while(spinlock1_set()==false){}} else { while(s_spinlock2_set()==false){} 
} 
log(runr+: Set spinlock 1. Now sleeping... );
blink(1000); log(runr+: Finished sleeping. Now setting spinlock 2... (if 
bug exists it will hang here...) );
if (test!=0){while(spinlock2_set()==false){}} else { while(spinlock2_set()==false){} 
}
log(runr+: Spinlock 2 set. Now unsetting both spinlocks... );
if (test!=0){spinlock2_rel(); spinlock1_rel();}else{s_spinlock2_rel(); 
s_spinlock1_rel();}
log(runr+: Completed. );  }}  }

BTW, my email has changed. 
[EMAIL PROTECTED] -- [EMAIL PROTECTED]
Thanks.
-Ys-

Free, encrypted, secure Web-based email at www.hushmail.com