aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes THreads Problem Complex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "THreads Problem Complex" Watch "THreads Problem Complex" New topic
Author

THreads Problem Complex

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634





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


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Tom Jarray
Greenhorn

Joined: Oct 05, 2010
Posts: 1
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

Joined: May 18, 2010
Posts: 634

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

mohitkumar gupta wrote:so,moderators

Why moderators ? Every rancher has the right to answer.


[My Blog]
All roads lead to JavaRanch
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

sorry, i meant ranchers
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
I for one would be more interested in looking at your questions if you bothered to format your code better


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

thanks Bert Bates for the advice
i have formatted the code .
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

please somebody reply
i have properly formatted the code
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

please somebody reply
i have properly formatted the code
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: THreads Problem Complex
 
Similar Threads
Thread with same instance as lock Query
Concurrency Gurus, It's for you!
PRoblem On threads
Callin start from run method In case of threads Problem
Threads using two different instances Problem