Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Doubt

 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic