Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-23-2012, 03:43 PM   PM User | #1
guptaraul
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
guptaraul is an unknown quantity at this point
Post Threads in Java, Help me out

This is my source code
[CODE]
public class Thread2 implements Runnable{
public void run()
{
System.out.println("run().");
throw new RuntimeException("Problem");
}


public static void main(String[] args) {
Thread t=new Thread(new Thread2());
t.start();
System.out.println("End of Method");

}
}
[CODE]
I am getting 2 different outputs

O/p 1.
[CODE]
End of MEthod
run().
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
at Thread2.run(Thread2.java:6)
at java.lang.Thread.run(Unknown Source)
[CODE]
O/p 2.
[CODE]
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
at Thread2.run(Thread2.java:6)
at java.lang.Thread.run(Unknown Source)
End of MEthod
run().
[CODE]

Can anyone help me, why am I getting two different outputs?
guptaraul is offline   Reply With Quote
Old 08-23-2012, 05:33 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The threads aren't synchronized, so there is no guarantee that the Runnable.run() will beat the main's println. Since you've tossed an exception, it must push that exception from the run into the main so that the main thread has to deal with the exception itself.

You'll need to read up on concurrency, here's the lesson's from the oracle tutorials: http://docs.oracle.com/javase/tutori...ncy/index.html
There are a number of methods, both manual and built in. I typically thread manually where I force explicit blocks on threads and notify when complete.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:45 AM.


Advertisement
Log in to turn off these ads.