• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

THreads Problem Complex

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Ist output:


mainYEs
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
Thread-0threadNO

I understand the above output JVM first chooses main and then thread-0 and then again main but i cannot understand the second output

2nd output:

mainYEs
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
Thread-0threadNO

It appears that JVM selects thread-0 but executes only first statement of run method of thread-0 without the second line of run and then then chooses main and after executing its println 10 times ,it returns back to thread-0

Queries:

q1.IIs my interpretation about second output correct??
q2.shouldn't there be Interrupted Exception in the second output??

------------------------------------------------------------------------------------------------------



i replaced the code with synchronized run method,still main thread is able to interrupt the thread-0




Third output:


mainYEs
mainNO
mainNO
Thread-0threadNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO

-----------------------------------------------------------------------------------------------------

then,i replaced it with the following code



still,the same output


-------------------------------------------------------------------------------------------

Q3.how can i get the following output at all times..


Fourth output:

mainYEs
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
mainNO
Thread-0threadNO

 
Tom Jarray
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String arg[])
{

Thr abc=new Thr();
new Thread(abc).start();
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+""+abc.as);
}
try {
abc.join();
} catch (InterruptedException e) {

}

}
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,ranchers,moderators

can anyone answer my questions ?

q1.Is my interpretation about second output correct??
q2.shouldn't there be Interrupted Exception ??


and TOM please use tags
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:so,moderators

Why moderators ? Every rancher has the right to answer.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i meant ranchers
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I for one would be more interested in looking at your questions if you bothered to format your code better
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Bert Bates for the advice
i have formatted the code .
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please somebody reply
i have properly formatted the code
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please somebody reply
i have properly formatted the code
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EaseUp. There may be some reasons why nobody answers :

1. The code is unreadable.
2. We don't understand the question. 1st and 2nd Output are the same. Personally, I don't want to look further when I see two similar outputs, with one comment saying "I understand" and the second comment saying "Why"...
3. The post is too long. Ask one question first.
4. Nobody knows the answer.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:EaseUp. There may be some reasons why nobody answers :

1. The code is unreadable.
2. We don't understand the question. 1st and 2nd Output are the same. Personally, I don't want to look further when I see two similar outputs, with one comment saying "I understand" and the second comment saying "Why"...
3. The post is too long. Ask one question first.
4. Nobody knows the answer.

I repeat again this! I think, the reasons are 1,2 and 3.
The code is unreadable.
The post is too long. Ask one question first.
We don't understand the question. Mostly, you are fluctuating with your answers also!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic