File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread blocking! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread blocking!" Watch "thread blocking!" New topic
Author

thread blocking!

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
Hi! one doubt on threads-> In a synchronized method, if a thread gets blocked for some input then does it release the lock and give a chance to another thread to run??
ashok.
Porchelvi Vendhan
Greenhorn

Joined: Sep 08, 2001
Posts: 21
Hi!
In a synchronized method if a thread needs an input it calls the wait() method and gives up the lock.So another thread will have a chance to lock the method.The thread which called wait() will have a chance to run when some other thread calls notify() or notifyAll().
I have explained as per my understanding of synchronized methods.
Please correct me if I am wrong.
Porchelvi
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I think Ashok meant, by writing "...if a thread gets blocked...", that some thread was waiting for some IO read method to return something. This is called "being blocked". In the case of IO blocking, the Thread instance gives up locks.
Correct me if I'm wrong
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
hi valentin and porchelvi! i think in the case when a thread is waiting for some input in some synchronized block then it waits and doesn't give up the lock until it finishes that block. Try this code:
correct me if i am wrong!
ashok.
__________
import java.io.*;
class cool extends Thread {
public synchronized void run() {
System.out.println("in run");
try {
System.in.read();
} catch(IOException e) {}
System.out.println("after run");
}
public static void main(String args[]) {
cool c = new cool();
new Thread(c).start();
new Thread(c).start();
}
}
galen wang
Greenhorn

Joined: Sep 21, 2001
Posts: 13
Hi ashok, you are right!
a thread running in a region of synchronized code won't give up the mutually exclusive lock of this object even though it gets into a non-runnable state (waitting, sleeping, blocked), it only gives up the CPU time when it gets into the non-urnnable state. in this situation the other thread can be run if it needs not the same mutually exclusive lock.
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
Hi...
In the code snippet provided 2 posts earlier, is it possible to create threads with the kinda constructor shown:
cool c = new cool();
new Thread(c).start();
new Thread(c).start();
Here in the 2 lines, a Thread object (c) is passed as an argument to the Thread constructor. I thought you're supposed to pass a Runnable object to this Thread constructor.
Please explain.
Elizabeth Lester
Ranch Hand

Joined: Aug 14, 2001
Posts: 129
Hi,
Yes, you are right that you do pass a Runnable object to a Thread constructor.
Because the class Thread implements Runnable (and therefore is a Runnable object too), I believe that the code below will work--even though it's overkill and the following will also work:

Others please correct me if I am wrong on this.
Hope that helps!
--liz

------------------
Elizabeth Lester
SCJP Dreamin'
[This message has been edited by Elizabeth Lester (edited September 27, 2001).]
galen wang
Greenhorn

Joined: Sep 21, 2001
Posts: 13
Hi Elizabeth,
below has 3 pattern of code
A)
cool c = new cool();
new Thread(c).start();
new Thread(c).start();
b)
cool c = new cool();
c.start();
c.start();
c)
new cool().start();
new cool().start();
they are different.
as A), two threads are created and started, both of them call the same run() method which belong to the one object c (although c itself is created but not be run). the synchronize acted on method run() is valid.
as B), first a thread object c is created and then it started to run. but at the second call of c.start(), an exception is occured
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at cool.main(cool.java:17)
that's because when you call start() if the thread was already started then IllegalThreadStateException will be throw. (ref API)
after this exception, althoug the thread c still alive but the caller thread will die.
as C), two threads are created and started, but they are two independent objects. although the run() method has a synchronize modificator, the synchronize won't be valid in this situation.
galen
Elizabeth Lester
Ranch Hand

Joined: Aug 14, 2001
Posts: 129
Hi Galen,
Thank you very much for that explanation!
--liz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread blocking!