• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads-synchronized blocks,Valentine &Michael and Others,Help needed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
1. I have one doubt..
at comment //1.Double inthe following code , If I replace InterruptedException with Exception.. I got error..Like found:Exception, required: Throwable.. why this is happening..
InterruptedException is subclass of Excpetion.
2. I am not able to understand clearly how does the synchronized blocks works.. Can you explain the following code and with different situations.. So it will clear to me.. please help me because i am writing JCP in two days..

thanks in advance,
Suresh
[This message has been edited by suresh seeram (edited October 19, 2001).]
[This message has been edited by Marilyn deQueiroz (edited October 19, 2001).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I replace InterruptedException with Exception.. I got error..Like found:Exception, required: Throwable.. why this is happening..
InterruptedException is subclass of Excpetion.

<pre>
class Reetrant{
String s=new String();
public void a(){
System.out.println(Thread.currentThread().getName());
synchronized(this){
try{
Thread.sleep(1000);
}catch(InterruptedException e){ //1.Doubt
}
< snipped >
}
</pre>

Maybe you spelled Exception wrong. I'm not getting any error when I substitute Exception for IOException

[This message has been edited by Marilyn deQueiroz (edited October 19, 2001).]
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Marilyn deQueiroz ,
thanks for your response.. Still I am getting error..There may be error in my editor or something..
I think this is not important for JCP exams..
Could you please explain codes 2 and 3..

thanks in advance..
regards,
Suresh
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried with Exception and did not get any error.
Now for first prog:
<pre>
class Reetrant{
String s=new String();
//String thrdName ;
public void a(){
String thrdName;
thrdName = Thread.currentThread().getName();
//synchronized(thrdName){
System.out.println(thrdName);
synchronized(this){
try{
System.out.println(thrdName + ": B4 sleep()");
Thread.sleep(1000);
//wait(100);
// Thread.yield();
}catch(Exception e){
//1.Doubt
}
b(thrdName);
//b();
System.out.println(thrdName + ": In a");
synchronized(s){
System.out.println(thrdName + ": In syn block");
}
Thread.yield();
}// end synch block
//}
synchronized(this){
try{
System.out.println(thrdName + ": B4 sleep() after first sync and in second sync");
Thread.sleep(1000);
//wait(100);
}catch(Exception e){
//1.Doubt
}
}
System.out.println(thrdName + ": end of a()");
}// end if method a()
public void b(String thrdName){
//public void b(){
System.out.println(thrdName + ": in b WT sync");
//synchronized(thrdName ){
synchronized(s){
System.out.println(thrdName + ": in b");
}
//}
}
} // end of Reetrant
class Synchr1 implements Runnable{
Reetrant r1 = new Reetrant();
public static void main(String args[]){
Synchr1 s1=new Synchr1();
//Synchr1 s2=new Synchr1();
Thread t1=new Thread(s1,"FirstThred");
Thread t2=new Thread(s1,"SecondThread");
//Thread t2=new Thread(s2,"SecondThread");

t1.start();
t2.start();
}

public void run(){
r1.a();
}
}

</pre>

you can remove comments and then put comments on yield() and/or wait() and/or sleep() to see what happens...
Now when you synchronize an object for a block then same object can not run the same block of statement.
sleep() does leave the lock so even a thread is sleeping it does not realease the lock.
but if you try wait(long time) or yield() you can find that other thread starts executing.
condition is that object should be same.
if you have s2 object reference then there will be no problem as there will be two different objects.

for ur second program:
there are three (if u include main thread also then 4)threads are running. one which start in main(), other two are starts by the thread u created. They are created by using annonymous class.
code is like
<pre>
new Thread() // no name for the class
{ public void run() { // overriding method run() for annonymous class
// some code
}.start() // register with the thread schedular( call the run() )
</pre>
correct me if wrong.

[This message has been edited by ravish kumar (edited October 19, 2001).]
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic