Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

tom Su

Greenhorn
+ Follow
since May 05, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by tom Su

public void start() {
if (runner == null) {
runner = new Thread(text);
runner.start();
}
You are not dealing with recursive topics here.. Calling a method
by itself may become an infinte-calling loop and could cause JVM to run out of control, sometimes..
println(i+j) will print out only one number = i+j,
println(""+i+j) will print out both i and j as if they were two Strings.
println(i+"'+j) may five you a comilation error at front. or runtimeerror
care to explain. what is a Green thread?
Gaus.. show us a little piece of your code.. maybe we could do some diggingggg
Guess what.. when I go one step further to name each thread, no more need for synchronizaion..
Multi-threading are kicking good without any more over-run..
package synchron;
public class synThread1 {
public static void main(String args[]) {
(new Thread(new Xyz(" a"))).start();
(new Thread(new Xyz(" b"))).start();
(new Thread(new Xyz(" c"))).start();
(new Thread(new Xyz(" d"))).start();
(new Thread(new Xyz(" e"))).start();
(new Thread(new Xyz(" f"))).start();
(new Thread(new Xyz(" g"))).start();
(new Thread(new Xyz(" h"))).start();
System.out.println("after all threads start ");
}// main
}
class Xyz implements Runnable {
int i = 5;
String sa = "";
Xyz(String sb) {
sa = sb ; }
public void run() {
i = 35;
while (true) {
for(long j=0;j< (long)(Math.random()*1.e9); j++) { double dd =Math.log(2.5);}<br /> System.out.print(sa+" i = " + i++ + " " );<br /> if ( i == 50 ) {<br /> System.out.println(sa +" stop at lower bound i = " + i);<br /> break;<br /> }<br /> if ( i > 100 ) {
System.out.println( sa +" stop at higher bound i = " + i);
break;
}
}// while
}
}// run
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
I come across this funny code, to stop it ocer-run, I have to do synchronized on the run() method. My Qs are.. 1) what's exact cause of over-run 50? 2)how to use block syn (synchrinzed(this) to stop this over-run?
package synchron;
public class synBlock {
public static void main(String args[]) {
Xyz r = new Xyz();
(new Thread(r)).start();
(new Thread(r)).start();
(new Thread(new Xyz())).start();
(new Thread(new Xyz())).start();
(new Thread(r)).start();
(new Thread(r)).start();
(new Thread(new Xyz())).start();
(new Thread(new Xyz())).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 /> // synchronized on the block fails<br /> // synchronized on the statement System.out.print fails too<br /> synchronized (this) {<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