Win a copy of High Performance Python for Data Analytics this week in the Python forum!

Mukesh Rathod

Ranch Hand
+ Follow
since Oct 11, 2000
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 Mukesh Rathod

Hi Prakash
Thanks for the information, really good and useful.
regards,
Mukesh
20 years ago
Hi all,
I am in the process of learning Servlets. Can anyone tell me how to use Applet-Servlett combination?
thanks in advance.
Mukesh
20 years ago
Hi ,
I am new to this particular discussion topic.
Yes, I would be glad to work on the project.
Contact me at : mtrathod@yahoo.com.
pl. let me know, if Ican contribute to this project.
Mukesh
20 years ago
Hi Jerry
Thanks a lot for good explanation.

Mukesh
Hi Sean & Cindy
Thanks for reply.
Actiually I also never heard such term in JAva. But as I am new to this field, I thought I might not be knowing it.Thanks again for clearing the doubt.

Mukesh
20 years ago
Hi all,
Can you please tell me what are Dynamic Data Structures in Java??
I was asked this question in interview.
Thanks in advance.
Mukesh
20 years ago
Hi Rahul,
Thanks for repy.
Can you please give me some link to reading material on Process?
mukesh

Can anyone tell about the difference between a Thread and a process?
I was asked this question in one of the interview.
Thanks in advance.
Mukesh
Hi Thomas,
what you r doing is synchronizing the thread on other object (b). Well, whenevr the thread releases the lock , it releases the lock on current object, lock on any other objects are not released. So instead of synchronized(b) try, synchronized(this), then once the first thread waits for Input, the second will print 0 to 9.
Hope this is clear now.
Mukesh

Hi Aruna,
In java technology, threads are usaually preemptive, howevr, this depends on underlying OS. (refer RHE)
The model of preemtive scheduler is that many threads might be runnable, but only one thread is actully running. This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable. In later case, the lower priority thread is preeemted by the thread of higher priority, which gets a chance to run instead.
Where as in timeslice, each thread gets an equal amount of time to execute.
Mukesh.

[This message has been edited by Mukesh Rathod (edited October 20, 2000).]
Hi Matt,
I hope whatever viji is saying is clear to u. In first case, as I mentioned, the variable i is initialized to 0 in th erun method itself. So whenever any thread starts running, it will reset the value of i to 0. Now look at second case, the value of i is not initialized in run method, instead it is initialized at time of declaration. Hence it will print values 0 to 9 only, irrespective of any number of threads running on same object.
Hope I am clear.
Mukesh
Its not strange reply. The method invocation is done dynamically in java, not the instance varibale. Hence, when u call a method,the method will be called depending on to which object (class) the reference var. is pointing to, while the varible will be called based on the type of the reference varible.
Mukesh

I guess many people have fear about change in exam pattern. So let me share my experience.
In the new exam, most (75-80%) questions are code based, not sure what was % of code based quest. The syllabus (objectives) is same. Also, for the multiple answer questions, you are told the exact numbers to be selected. I found this very useful, infact it makes life easier than before. One has to be careful about time, though it is more than sufficient.About no of questions, I have already mention whatever I could remember, in my earlier reply (see above)
Hope this will be useful.
Mukesh
Hi Aruna,
Hi Matt, Viji, hope this would also clarify your doubt.
Let's examine the cases one by one.
Case 1: As given.
Here the variable i is instance variable, hence if two threads are created using same object, both the thread will be referring to the same variable, so both the threads are updating the value of the variable and the result would be dependent on the underlying operating system.
Let�s consider timeslice (which I am using). First thread will be started, assume it continues execution for a while, then it will initialize the variable i = o, then increase the value of i and prints it. Now thread 2 will be started in between, it again initializes the value of i = 0 ( as i is initialized in run method) , then start printing the increasing values. Let�s assume, now again the first thread takes control, then it will continue with the value of i, where the second thread left (as both thread are referring to same object). Hence hereafter, whether the first thread is running or the second, they will print the incremental value of i until i becomes 10 i.e. last value printed would be 9.
The output should look like
Value of i = 0 // first thread is running
Value of i = 1
Value of i = 2
Value of i = 0 // second thread has started.
Value of i = 1 // either first or second thread is running.
Value of i = 2
Value of i = 3
Value of i = 4
Value of i = 5
Value of i = 6
Value of i = 7
Value of i = 8
Value of i = 9
On pre-emptive system, the first thread continues, prints 0 to 9 values, until it completes the execution and then second is started and again prints 0 to 9. (not sure about this behavior, as I haven�t tested it)
Case : 2 : Initialize the value of i at declaration time.
In this case, the part of code will look like,
class Thread1 implements Runnable{
int i = 0; // or int i;

public void run(){

while(true){ //rest of code
}
}// end of run
} // end of class
Here, the value of i will be initialized only once, when the object is created. Hence, no matter what is the underlying OS, no matter which thread is running, it will print values from 0 to 9 only once. So it will always print 10 values.

Case 3: variable i is local variable, instead of Instance variable.
The part of code now will look like
class Thread1 implements Runnable{

public void run(){

int i =0;
while(true){ //rest of code
}
}// end of run
}// end of class
Here, i is local variable of run method. Hence, both the threads keep separate copy of the variable. So both the threads will print value of i from 0 to 9. Hence, total 20 values of i will always be printed, the order of printing will be determined by which thread is running and which in turn depends on underlying OS.
Hope I am clear.
Mukesh
[This message has been edited by Mukesh Rathod (edited October 20, 2000).]
Hi All
I got too many mails. Thanks all of you. Almost everyone is asking me about threads. The concern about threads could be understood, as it seems the toughest. So I thought I might share whatever little I could, regarding thread in the benefit of the group.
The thread has various states, new state, Runnable (ready-to run), Running, Blocked, Waiting pool, Lock pool, dead etc. (I have a nice diagram showing various stateo fthread, but I am unable to copy it here. Any Help!!!)
New State
When a thread is created, it is said to be in new state. Remember, when a thread is created, it does not start execution. The thread can be created by either creating a subclass of java.lang.Thread or by creating a class, which implements the interface java.lang.Runnable. In either case, override the public void run() method and define the code which you expect to be executed while the thread is running. REMEMBER THE SIGNATURE OF THE run() METHOD.
When the thread is in new state, the isAlive() method will return false. When the thread is created, its priority is set to the priority of the thread which created the thread.
Runnable (Ready-To-run)
When start() method called on new thread, the thread becomes Runnable. Remember that the thread does not start execution when the start() method is called. It may happen that it may not start forever (another running thread calls System.exit ()).
Remember, the start() method dose not call the run() directly, the Java Virtual Machine calls the run method of this thread. What it does is probably register the thread with thread scheduler (RHE). It throws IlleagleThreadStateException , if the thread was already started.
There are other scenarios also, where a thread becomes Runnable, which we will see later.
Running
When the thread is running, it executes code which is defined in the run() method. When the thread will run, can never be guaranteed. This depends on thread scheduler and which, in turn, depends on the underlying OS.
The model of preemptive scheduler is that many threads might be runnable, but only one thread is actually running. This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable. In case of timesliced, equal amount of CPU time is given to each thread.
The JVM exits when all the working threads daemon threads or System.exit(0) is called.
Now consider the following code. Try to compile and run this code. How many times the value of i is printed? Now initialize the value of I at declaration time only and see the result. Now declare the variable i in the method itself instead of making it instance variable. See the result. This will give an idea about different possibilities regarding value of instance variable which are changed by different threads.
class TestThread{
public static void main(String args []){

Thread1 t1 = new Thread1();
Thread th1 = new Thread(t1);
Thread th2 = new Thread(t1);
th1.start();
th2.start();
}
}// End of TestThread
class Thread1 implements Runnable{
int i;
public void run(){

i = 0;
while(true){
System.out.println("Value of i = " + i++);
if ( i == 10)
break;
}
}
}
Dead Thread
When the execution of the run() method is over, the thread becomes dead thread. The dead thread can�t be started again. However the variables and methods of the dead thread can be accessed.
The stop() method also make the thread dead. Also if an exception is thrown and is uncaught, the thread becomes dead.
The isAlive () method will return false, if the thread is dead. Hence, the isAlive() method returns false only if the thread is in new state or is dead. All other time it returns true.
yield() � This is a static method. Use Thread.yield() to give other threads of the same priority a chance to execute. If other threads at the same priority are runnable, yield() places the calling thread into the Runnable pool and allows another thread to run. If no other threads are Runnable at the same priority, yield does nothing.
However, RHE mentions that there is no guarantee that when yield () is called, the thread with lower priority will not run. I tested the code, and it is found that the running thread doesn�t give up when yield is called and when only running threads are ones with lower priorities.
Blocked Thread
The Running thread can be blocked in many ways.
sleep(long time) - This is static method. It stops execution of the current thread and blocked for atleast the time as specified if it is not interrupted. The thread does not lose ownership of any monitors. Hence when the specified time is elapsed or the thread is interrupted, the thread becomes Runnable (not Running).
join() � The join method causes the current thread to wait until the thread on which the join method is called terminates or is interrupted or the specified time as argument to join method elapses.
I/O operation � if an executing thread calls any method which requires some input stream to be responded, then the thread waits for such input and hence is blocked. Any such blocking of thread will ensures that it releases the lock.
You may refer to following link to understand object lock, wait, notify, and interrupt, synchronization etc., as it provides very good explanation. Hence I thought no need to repeat the same.
http://www.javaranch.com/maha/Discussions/Threads/threads.html
Following are some of the things, which I would like to highlight:
When a thread receives notify/notifyAll , it goes to Object�s Lock Pool i.e. waits for the object�s lock, and once it acquires the lock, it becomes Runnable.
Calls to interrupt() are stored i.e. if interrupt() is called on a thread which is not blocked by sleep, wait, join etc. and doing something else, then it will continue with the work. When the thread, on which interrupt () was called, calls any of the above-mentioned methods, it will be interrupted immediately.
Calls to notify are not stored. A notify call can be issued without regard to whether any threads are waiting. If the notify method is called on an object when no threads are blocked in the wait pool for that object�s lock, the call has no effect.
When notify call is executed on a particular object, an arbitrary thread is moved from object�s wait poll to a lock pool. There is no way you can ensure that a particular thread will be moved out of object�s wait pool.
Wait method releases the lock on the current object only. If it has also acquired locks on some other objects, it does not release those locks.

Sources: RHE, Student Guide from Sun Educational Services, API documentation, special thanks to Maha Anna for excellent explanation on this topic (see the above mentioned link)
Correct me wherever I am wrong.
Hope this will be of some use to all.
Mukesh

[This message has been edited by Mukesh Rathod (edited October 21, 2000).]