• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

THreads Problem Complex

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • 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

 
Greenhorn
Posts: 1
  • 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
Eclipse IDE Chrome Java
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i meant ranchers
 
author
Posts: 8995
19
  • 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
Eclipse IDE Chrome Java
  • 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
Eclipse IDE Chrome Java
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please somebody reply
i have properly formatted the code
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • 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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!