aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Synchronization (Urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Synchronization (Urgent)" Watch "Threads Synchronization (Urgent)" New topic
Author

Threads Synchronization (Urgent)

Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
Friends
The code below is frm a a person named Billesh Arora who had some questions on a post i failed to understand the 1st question can any 1 pls pls give me a detalied explanation of the code below:
public class Test extends Thread {
public synchronized void run() {
print();
}
public synchronized void print() {
System.out.print("JAVARANCH");
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
public static void main(String arg[]) {
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
}
The output is javaranch followed by javaranch without any time delay why is the time delay ommited
I am giving the exam on 29th so it is urgent alos whr can i find more of "is a" and 'has a " type of questions
Amit


<I>Chance Favours the Prepared minds"</I>
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

Amit,
The lock is on an object not the class. You are creating two
separate objects here hence one thread does not have to wait for the other one to finish.

Rgds
Sahir


....
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
<html><body>Hi! I have modified your code to show you how to make an object method a class method and the result you wanted:
<code>
public class Test extends Thread {
public synchronized void run() {
print();
}
public static synchronized void print() {
System.out.print("JAVARANCH ");
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
public static void main(String arg[]) {
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
}
</code></body></html>
faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
The delay factor is not visible cuz the print() method is run before calling the thread to sleep. However if you change the code as
public class Testsync1 extends Thread {
public synchronized void run() {
print();
}
public synchronized void print() {
System.out.print("JAVARANCH");
try {
Thread.sleep(5000);
System.out.print("JAVARANCH is good");
}
catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
public static void main(String arg[]) {
Testsync1 t1 = new Testsync1();
Testsync1 t2 = new Testsync1();
t1.start();
t2.start();
}
}
then u�d find a possible delay factor........
Hope this helps too
Faiza
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Synchronization (Urgent)
 
Similar Threads
thread urgent please
Urgent ..one more Thread ....
Threads & synchronization
Thread behavior with synchronized method
yield() on synchronized code