This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Its Simple ! Thread and Runnable ! Help me out ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Its Simple ! Thread and Runnable ! Help me out !" Watch "Its Simple ! Thread and Runnable ! Help me out !" New topic
Author

Its Simple ! Thread and Runnable ! Help me out !

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Hey techpals,


My Output :

Thread.currentThread() = Thread[Thread-0,5,main]
Inside Device_Production Class run Method

Thread.currentThread() = Thread[Thread-1,5,main]
Inside Device_Production Class run Method

Thread.currentThread() = Thread[Thread-0,5,main]
Inside Device_Production Class run Method

Thread.currentThread() = Thread[Thread-1,5,main]
Inside Device_Production Class run Method


My Question ! :roll:

[I]What is 0,5,main here ?
What is 1,5,main here ?

As per my understanding what happens is :

run[0] is a thread object and it has a copy of the object dop.
run[1] is also a thread object and that 2 has a copy of dop.

so, if there are variable and their values modified by the object with thread - run[0] will not affect the other run[1] at all cases except for static context !


Also,

its almost like saying
run[0] = new Thread(dop[0]);
run[1] = new Thread(dop[1]);

Even in this case variables used by dop[0] will not collide with dop[1]
both are seperate object instances and seperate copies.

In this case below:

where

run[0] = new Thread(dop);
run[1] = new Thread(dop);

'dop' - Both are same objects but seperate copies maintained with respect to thread run[0] and run[1]

so even in this case varibales used by dop, will not collide since they run as copies inside run[0] and run[1].

except for static context

Am i right about the above statements !


#When i say Class Device_production as runnable
and i have to run it as a thread

ObjectDevice_production = new Device_production();

Thread thr = new Thread(ObjectDevice_production);

thr.start();

Is this the only way,

So what is runnable.run();

When Should i say class to implement runnable

and say extends thread !

What design constraints impose a class to say implment runnable and extends thread ?


Please clarify me in this Scenario!

thanks guys.[/I]


Discussion - the powerfull way to excellence!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
Originally posted by ram kumar:
What is 0,5,main here ?
What is 1,5,main here ?


If you concatenate an object with a String, then the toString() method of that object will be called. You are concatenating a Thread object with a String, so you need to look at the Thread.toString() method to see what it does. The Javadoc for Thread class will tell you this.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Each thread has a name. If you don't specify it in the constructor, it will find one of its own. This name is "Thread-", followed by a unique number - starting at 0. So your threads are called "Thread-0" and "Thread-1".

The 5 in the thread print-out is its priority. Again, you can specify this (by calling setPriority), and if you don't, it's Thread.NORM_PRIORITY which happens to be 5.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
Originally posted by ram kumar:
When Should i say class to implement runnable

and say extends thread !

What design constraints impose a class to say implment runnable and extends thread ?


You should only extend Thread if your class is a specialised kind of Thread. You should implement Runnable when you want to be able to execute your class in a separate thread.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
Originally posted by ram kumar:

My Output :

Thread.currentThread() = Thread[Thread-0,5,main]
Inside Device_Production Class run Method

Thread.currentThread() = Thread[Thread-1,5,main]
Inside Device_Production Class run Method

Thread.currentThread() = Thread[Thread-0,5,main]
Inside Device_Production Class run Method

Thread.currentThread() = Thread[Thread-1,5,main]
Inside Device_Production Class run Method


[b]


Please make sure you post your actual code. That code will not produce the output you have shown. For s start it doesn't print 'Inside Device_Production Class run Method' anywhere.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Rob Prime:
Each thread has a name. If you don't specify it in the constructor, it will find one of its own. This name is "Thread-", followed by a unique number - starting at 0. So your threads are called "Thread-0" and "Thread-1".

The 5 in the thread print-out is its priority. Again, you can specify this (by calling setPriority), and if you don't, it's Thread.NORM_PRIORITY which happens to be 5.



joanne says am concatenating thread with string.
Is that what i am doing with that code exactly.
Hope am saying a thread to run over the object 'dop'
Would some one just give me a clear view.

If i say

Thread run[0] = new Thread(dop);
Thread run[1] = new Thread(dop);
run[0].start();
run[1].start();

Now, do both the threads run on the same object dop.
What happens to the variables and its values set at dop methods.
Do they maintain seperate copies for run[0] and run[1];
Please explain !

Also, could some one help me out with a clear simple scenario of when to use extends Thread and when to use Implements runnable.

Am clear about the definition but need a scenario to understand it conceptually.

thanks in advance

[ August 19, 2008: Message edited by: ram kumar ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
Originally posted by ram kumar:
joanne says am concatenating thread with string.
Is that what i am doing with that code exactly.


I was referring to this line of the codebecause you had asked a question about the output of it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
Originally posted by ram kumar:
If i say

Thread run[0] = new Thread(dop);
Thread run[1] = new Thread(dop);
run[0].start();
run[1].start();

Now, do both the threads run on the same object dop.
What happens to the variables and its values set at dop methods.
Do they maintain seperate copies for run[0] and run[1];
Please explain !


Both threads are referencing the same Device_production instance. As the same object is being used, any changes made to the Device_production object in one thread will be seen in the other thread.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Joanne Neal:


Both threads are referencing the same Device_production instance. As the same object is being used, any changes made to the Device_production object in one thread will be seen in the other thread.


Thats nice ! joanne.

I was hit strongly when you said threads and strings concatenation.

Thats okay!

Both threads are referencing the same Device_production instance. As the same object is being used, any changes made to the Device_production object in one thread will be seen in the other thread.


This is what i was looking for !

Thanks for the response !

 
wood burning stoves
 
subject: Its Simple ! Thread and Runnable ! Help me out !
 
Similar Threads
Reg. Some Thread basics
Instantiating a Thread page no 679
Understanding the Thread reference that is returned by call to Thread.currentThread();
KATHY SCJP 1.6 : THREADS, exercise 9-2. Is my code fine?
Thread synchronization