This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

thread synchronization fail  RSS feed

 
tom Su
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
continue on the previous issue..
synchroniztion on thread object does fail too..
package synchron;
public class synThread1 {
public static void main(String args[]) {
Xyz r = new Xyz();
Thread t1 = new Thread(r);
synchronized(t1) { t1.start();}
Thread t2 = new Thread(r);
synchronized(t2) { t2.start();}
Thread t3 = new Thread(r);
synchronized(t3) { t3.start();}
Thread t4 = new Thread(r);
synchronized(t1) { t4.start();}

System.out.println("after all threads start ");
}// main
}
class Xyz implements Runnable {
int i = 5;
public void run() {
i = 35;
while (true) {
for(long j=0;j< (long)(Math.random()*1.e8); j++) { double dd =Math.log(2.5);}<br /> System.out.print("i = " + i++ + " " );<br /> if ( i == 50 ) {<br /> System.out.println(" stop at lower bound i = " + i);<br /> break;<br /> }<br /> if ( i > 100 ) {
System.out.println(" stop at higher bound i = " + i);
break;
}
}// while
}
}// run
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!