IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i have question in Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "i have question in Thread" Watch "i have question in Thread" New topic
Author

i have question in Thread

jong hoon baek
Greenhorn

Joined: Oct 17, 2000
Posts: 2
i'm so panic these days..
next monday i 'll take exam..
1.Synchronized methods if they called in sequence what will be the output?
Synchronized somemethod1(){
while(true){
x++;
y--;
System.out.println("x:"+x+"y:"+y);
}
}
Synchronized somemethod2{
x--;
y++;
System.out.println("x:"+x+"y:"+y);
}
2.Second question may be like same but the methods are not Synchronized ,what will be the output.
3.Threads calling sequence with start method or calling straight run() method what will be the error or just the current thread will be running.

plz help me..
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Mr. jong hoon baek , the one thing you MUST avoid when taking the exam is panic.Just keep your cool , and think clearly and u will get the answer.It's just a matter of getting the fundamentals right.
1)In the first case , if a thread obtains a lock on the object of the class containing the method somemethod1(),then as a while(true) loop is being executed , execution will go on FOREVER and no other thread will be able to obtain a lock on the object.No other thread will be able to call somemethod2() also for the same reason.
2)In the second case , since methods are not synchronized , any thread will be able to call both somemethod1() or
somemethod2().

3) Threads are normally utilized when PARELLEL EXCECUTION is desired.This means 2 parts of a program executing simultaneously.A parellel path of execution is created by constructing a thread(I will not go into the details here) and calling it's start() method.This will register the thread with the THREAD SCHEDULER and make it eligible to run.Meanwhile , the other part of the program will continue to run.If u call the run() method directly , the calling part will block and WILL NOT CONTINUE until the run() method returns.This is not parellel execution.
U have answered the question in the question itself.Only the called part of the program will be executing if run() is called directly.


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
 
IntelliJ Java IDE
 
subject: i have question in Thread
 
Threads others viewed
can a thread having lock of object, simulatenously invoke TWO sunchronized methods ?
Enthuware question doubt
Thread Synchrinization
Synchronization and Lock Confusion - Enthuware Sample
2 quick thread verifications
developer file tools