Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes Thread Vs. Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Vs. Runnable" Watch "Thread Vs. Runnable" New topic
Author

Thread Vs. Runnable

Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Hi All.
Here's my Question...
If I want to create two threads on the same Object, can I do so by extending the Thread class ?
Here's why I ask this question...
My sample code...
Case 1 - Extending Thread class
-------------------------------
public class myThread extends Thread {
public void run() {
synchronized(this)
{
// Some code... }
}
}
public static void main(String[] args) {
Thread t1 = new myThread(); // Thread #1
Thread t2 = new myThread(); // Thread #2 BUT on a different object, not the same one as the first one.
t1.start();
t2.start();
}
}
Case 2 : Implementing Runnable
------------------------------
class MyRunnable implements Runnable {
public void run() {
synchronized(this)
{
// Some code... }
}
}
public class MyThread {
public static void main(String[] args) {
MyRunnable myRun = new MyRunnable();
Thread t1 = new Thread(myRun); // New Thread on myRun object
Thread t2 = new Thread(myRun); // Another Thread on the SAME myRun object
t1.start();
t2.start();
}
}
------------------------------------------------
In both these code samples, I want to synchronize a block of code on the object referenced by 'this'. In order to achieve object synchronization, I need to have multiple threads on the SAME OBJECT ( At least thats what my concepts are about synchronization ). Hence, my Question ... Is there a way I can create 2 or more new threads on the same object using Thread class ? If one takes a look at the Thread class, although there is a constructor that takes a Runnable reference... which makes it possible for Case 2 to work the way I want it to.
Am I missing something in the Thread class ? I am sure I am... 'cause I know that although Runnable is a better way of creating threads, the second way of extending from the Thread class works as well.
thanks for your help in advance.


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Yes it is true, the two threads in the first example are not properly synchronized. It is necessary to be aware of which object "this" is pointing to when synchronizing. This one of the possible reasons to use an object solely as a lock for synchronization instead of "this".


SCJP2. Please Indent your code using UBB Code
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
But if the entire run() method of MyRunnable is synchronized, what's the point of having two threads? Only one will run at a time.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Vs. Runnable
 
Similar Threads
Threads blocking on static method.... or do they?
synchronization
Practice question on synchronized
question on wait() from K&B master exam
Threading synchronization