*
The moose likes Threads and Synchronization and the fly likes Thread Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Doubt" Watch "Thread Doubt" New topic
Author

Thread Doubt

subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
hi,
Lets say I have a class.

class A extends Thread{
public static void main(String[] args) {

A a = new A();
A b = new A();
Thread a = new Thread(a);
Thread a1 = new Thread(b);

a.start();
a1.start();
}
public synchronized void run() {
........
.............
}
}


Can Threads a and a1 simultaneously access run?
I think, they can because they acquire lock on different objects, please tell me if i am correct.

Thanks,
subhadeep
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Since you actually have code, what did you get when you tried it? Obviously, you need something in the run() method that helps determine if it is running in parallel or not.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
hi,
public synchronized void run() {
try {
Thread.currentThread.Sleep(3000);
}catch() {..}

for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread.getName());
}
}

What i see is :-
1) 2 threads from the same thread do not interfare.So i waits until other completes.
2) 2 threads from different object interfere.
Am i going wrong somewhere, or is it correct?

Thanks,
subhadeep
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
Hi Henry,
I have tested this program and i see that " two different threads from two objects can simultaneously access a synchronized method.I just want to be sure if i am doing right(what is the reason behind such a rule) or am i doing something wrong? (because we cannot really rely on threads)"
Please help me out.Anxiously waiting for your reply.

Regards,
subhadeep
subhadeep chakraborty
Ranch Hand

Joined: Jul 29, 2006
Posts: 67
hi,
I think i was wrong with including the sleep() method as it allows other threads to run but i have tested without the sleep method and i see the same issue.


public synchronized void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread.getName());
}
}

Regards,
subhadeep
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

I have tested this program and i see that " two different threads from two objects can simultaneously access a synchronized method.I just want to be sure if i am doing right(what is the reason behind such a rule) or am i doing something wrong? (because we cannot really rely on threads)"


Two threads must lock on the same lock in order to block on each other. So, if they are using two different locks, then they are not synchronizing, with each other.

Which from your original post, you already know... and have just confirmed. So why do you think threads is not reliable?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

I think i was wrong with including the sleep() method as it allows other threads to run but i have tested without the sleep method and i see the same issue.


I don't know why you consider this an "issue". You already stated in the original post that this shouldn't work, because the locks are different. You confirmed that it didn't work. And now, you have an issue? It worked exactly the way you described it.

And BTW, sleep() doesn't release any locks. I think you may have gotten it confused with wait().

Henry
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Originally posted by subhadeep chakraborty:
Can Threads a and a1 simultaneously access run?
I think, they can because they acquire lock on different objects, please tell me if i am correct.


I don't see any locks at all in your code. Simultaneous depends, on a single processor system, they will timeshare. On a dual or quad core box, they will literally run simultaneously.

I strongly recommend Henry's book: Java Threads, 3rd Edition,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Doubt
 
Similar Threads
need help with inner classes
another of Dan's question
Thread question
Thread's Beta Version (Dan)
not at all able to understand what is happening in this program