Results 1 to 7 of 7

Thread: Multi Threading

  1. #1
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Multi Threading

    I get this error while compiling..can anybody help me?

    LabEx3.java:4: error: invalid method declaration; return type required
    NewThread() {
    ^

    Here's my code
    Java Code:
    class MainThread extends Thread
    {
    	
    	NewThread() {
    	super("Demo Thread");
    	System.out.println("Child thread: "+this);
    	start();
    	
    	return;
    	}
    		public void run()
    		{
    			for (int i=5;i>=1;i--)
    			try{
    			{
    				System.out.println("Main Thread: "+i);
    				Thread.sleep(2000);
    			}
    			} catch (InterruptedException e)
    			{
    				return;
    			}
    			
    			System.out.println("Exiting Main Thread");
    		
    		}
    }
    
    class ChildThread extends Thread
    {
    	public void run()
    	{
    		for (int i=5;i>=1;i--)
    			try{
    			{
    				System.out.println("Child Thread: "+i);
    				Thread.sleep(4000);
    			}
    			} catch (InterruptedException e)
    			{
    				return;
    			}
    		System.out.println("Exiting Child Thread");
    		
    	}
    }
    
    class LabEx3
    {
    	public static void main(String args[])
    	//throws InterruptedException{
    	
    	{
    	
    		
    		MainThread obj1=new MainThread();
    		obj1.start();
    		
    		
    		System.out.println("First Thread is runnable");
    		
    		ChildThread obj2=new ChildThread();
    		obj2.start();
    		
    		
    		//System.out.println("SecondThread is runnable");
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Multi Threading

    What is the name of your class? Does your constructor carry that name?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default Re: Multi Threading

    Change NewThread() to MainThread() at line # 4.
    Rakesh Mehta

  4. #4
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Re: Multi Threading

    Thanks guys :) It works :)
    But I wondering what does super means in Line 5?

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,865
    Rep Power
    5

    Default Re: Multi Threading

    super = daddy.

    What do you mean what does it mean? You wrote that code didn't you? You didn't copy/paste it from somewhere and now try to make it work without making any effort to understand how and why it works, aren't you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Re: Multi Threading

    Yes that was my code.Its just I don't understand why do I need to call the super syntax.. I also understand the concept of inheritance in Java :)

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

Similar Threads

  1. Multi-Threading
    By TonyDaniels in forum New To Java
    Replies: 4
    Last Post: 03-09-2012, 06:16 PM
  2. Multi-Threading
    By TonyDaniels in forum Advanced Java
    Replies: 1
    Last Post: 03-09-2012, 05:52 PM
  3. Multi-threading issue
    By StevenTNorris in forum Advanced Java
    Replies: 2
    Last Post: 10-19-2011, 09:08 PM
  4. multi-threading problem
    By imorio in forum New To Java
    Replies: 1
    Last Post: 04-25-2011, 06:52 PM
  5. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 04:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •