Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Lost on Java Threads

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Lost on Java Threads

    Hello,

    Please, this question is from the OCP Java SE 6 Programmer Practice Exams textbook, Assessment Test 2 question 8. Below is the code for the question.

    public class Salmon extends Thread {
     
    	public static long id;
     
    	public void run(){
    		for(int i=0; i<4;i++){
    			if(i==2 && id==Thread.currentThread().getId()){
    				new Thread(new Salmon()).start();
    				throw new Error();
    			}
    			System.out.print(i + " ");
    			System.out.println("programme got past Error!!");
    		}
    	}
    	public static void main(String[] args) {
     
    		Thread t1 = new Salmon();
    		id = t1.getId();
    		t1.start();
    	}
     
    }

    When I ran the code, below is my output:

    0
    programme got past Error!!
    1
    programme got past Error!!
    Exception in thread "Thread-0" java.lang.Error
    at org.OCP.JSE6.Exercises.Assessment1.Salmon.run(Salm on.java:15)
    0
    programme got past Error!!
    1
    programme got past Error!!
    2
    programme got past Error!!
    3
    programme got past Error!!
    My questions are as follow:
    1.why was there an exception in thread after i was 1 and then the programme continued again. I just want to understand the output.

    2. I thought since there was an exception, the programme should crash but that wasn't the case, the programme continued after the new Error() statement. Why is this?

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Lost on Java Threads

    1: Because you're creating another Thread. Only the first Thread will hit the Exception.

    2: The Thread that encountered the Exception will exit. The rest will not.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    help_desk (August 7th, 2014)

Similar Threads

  1. Lost In The World Of Java
    By pagan1904 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2013, 03:17 PM
  2. Can I use Java for recovering lost text messages from my cell phone?
    By lavaziniamarcu in forum Java Theory & Questions
    Replies: 3
    Last Post: March 12th, 2012, 02:17 PM
  3. Creating a Gun in Java, I'm completely lost.
    By Pryde in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2011, 08:13 AM
  4. [SOLVED] java Reflection Question - I am lost.
    By prain in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2010, 02:43 PM
  5. please could you help me with this java problem im very lost
    By eyeore in forum Java Theory & Questions
    Replies: 4
    Last Post: September 7th, 2009, 09:19 AM

Tags for this Thread