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: Working with threads

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Working with threads

    My aim was to creat e a program that performs the following:
    o Create two threads
    o One thread asks the user to enter groups of 5 numbers
    o As soon as 5 numbers has been entered, the other thread computes it sum
    o This continues for each group until the end of input is reached

    I wrote the following code but itsd just not running, I guess I have not been able to codea good logic for them to synchronise please help!!!

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
     
     
    public class Thrd1 implements Runnable {
    	public static int wakeup()
    	{
    		return 1;
    	}
    	static int Numbers []= new int[5];
    	int counter=0;
    	public void run()
    	{
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String temp=null;
    		try
    		{
    		System.out.println("Enter 5 numbers:");
    		for(int i=0;i<5;i++)
    		{
    		System.out.println("Enter "+(i+1)+" numner:");
    		temp=br.readLine();
    		Numbers[i]=Integer.parseInt(temp);
    		}
    			wakeup();
    		}
    		catch(Exception e)
    		{
    			System.out.println("Error:"+e.getMessage());
    		}
     
    		}
     
    	}
     
    public class Thrd2 implements Runnable {
    	public static int wakeup()
    	{
    		return 1;
    	}
    	public void run()
    	{
     
    		int sum=0;
    		System.out.println("The sum of numbers is:");
    		for (int i=0;i<5;i++)
    		{
    			sum+=Thrd1.Numbers[i];
     
    		}
    		System.out.println("Sum is ="+sum);
    		wakeup();
    	}
     
     
    	}
     
    public class MainClass {
     
    	public static void main(String[] args) {
    		while (true) {
    			Thrd1 obj1 = new Thrd1();
    			Thread firstthread = new Thread(obj1, "1st Thread");
    			Thrd2 obj2 = new Thrd2();
    			Thread secondthread = new Thread(obj2, "2nd Thread");
    			if (Thrd1.wakeup()==1)
    			{
    				try {
    					firstthread.wait();
    					secondthread.start();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    			if (Thrd2.wakeup()==1)
    			{
    				try {
    					firstthread.start();
    					secondthread.wait();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    		}
     
    	}
    }
    Last edited by copeg; July 12th, 2010 at 09:54 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with threads

    its just not running
    Do you get errors?
    Can you explain what is happening and show the console output?

Similar Threads

  1. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  2. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  3. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM