Results 1 to 5 of 5
  1. #1
    sleepingpeace is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Should be simple answer...

    Below is the code for a race. 2 threads are instantiated and started... What I want is that when one reaches 1000 meters, it says I finished and calls the finished(); but I can't get it to call the finished(); as it's in another class. I want that finished method to also determine which thread called it and then interrupt the other thread. I've been staring at code for hours now and am drawing a blank, I'm sure you know what I mean. Thanks for the help.

    Java Code:
    public static void main(String[] args) {
    		Thread tortoise = new ThreadRunner("Tortoise", 0, 10);
    		Thread hare = new ThreadRunner("Hare", 90, 100);
    		
    		System.out.println("Get set... Go!");
    		tortoise.start();
    		hare.start();		
    	}
    	
    	public void finished() {		
    		if (Thread.currentThread().getName().equals("Tortoise")) {
    			System.out.println("The race is over! The Tortoise is the winner.");
    			hare.interrupt();
    		}
    		else if (Thread.currentThread().getName().equals("Hare")) {
    			System.out.println("The race is over! The Hare is the winner.");
    		}
    	}
    }
    
    class ThreadRunner extends Thread {
    	private String name;
    	private int restValue, speed;	
    	
    	ThreadRunner(String Name, int RestValue, int Speed) {
    		name = Name;
    		restValue = RestValue;
    		speed = Speed;
    	}
    	
    	public void run() {
    		int distance = 0;
    		while (!isInterrupted() && distance < 1000) {
    			try {
    				int rand = (int) (Math.random() * 100);
    				if (restValue <= rand) {				
    					distance += speed;
    					System.out.println(name + " : " + distance);
    				}		
    				Thread.sleep(100);
    			}
    			catch (InterruptedException e){				
    				break;
    			}
    		}	
    		System.out.println(name + ": I finished!");
    		finished();
    	}
    }
    Last edited by sleepingpeace; 02-17-2011 at 03:38 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Create an instance of the class that contains it and pass that reference value to both instances (in the constructor, of course) and then let them use that instance to call the method, or make the method static.

    Edit: You are, obviously, going to want to synch the method, though.

  3. #3
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default Reply

    First declare finished method static and call it with classname.finished()

    in finished method write outside all the condition

    System.out.println("name of the thread " + Thread.currentThread().getName());

    which will give the name of thread by seeing output you will be more cleared.

  4. #4
    sleepingpeace is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Perfect, static is exactly what I was looking for. So how do I interrupt the other thread when there's a winner in the finished();? Here's what I got so far.



    Java Code:
    public class Race {
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Thread tortoise = new ThreadRunner("Tortoise", 0, 10);
    		Thread hare = new ThreadRunner("Hare", 90, 100);
    		
    		System.out.println("Get set... Go!");
    		tortoise.start();
    		hare.start();	
    	}
    	
    	public static synchronized void finished(String runner) {	
    		if (runner.equals("Tortoise")) {
    			System.out.println("The race is over! The Tortoise is the winner.");
    		}
    		else if (runner.equals("Hare")) {
    			System.out.println("The race is over! The Hare is the winner.");
    		}
    	}
    }
    
    class ThreadRunner extends Thread {
    	private String name;
    	private int restValue, speed;	
    	
    	ThreadRunner(String Name, int RestValue, int Speed) {
    		name = Name;
    		restValue = RestValue;
    		speed = Speed;
    	}
    	
    	public void run() {
    		int distance = 0;
    		while (!isInterrupted() && distance < 1000) {
    			try {
    				int rand = (int) (Math.random() * 100);
    				if (restValue <= rand) {				
    					distance += speed;
    					System.out.println(name + " : " + distance);
    				}		
    				Thread.sleep(100);
    			}
    			catch (InterruptedException e){				
    				break;
    			}
    		}	
    		System.out.println(name + ": I finished!");
    		Race.finished(this.name);
    	}
    }

  5. #5
    sleepingpeace is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Fixed it, I just had to declare the threads outside of the main method in order to allow them to be static. Thanks for the help guys.

Similar Threads

  1. This will have an obvious answer!
    By MarkusHendersonicus in forum New To Java
    Replies: 6
    Last Post: 12-24-2010, 12:49 AM
  2. Could anyway answer me this?
    By Jojomofoman in forum New To Java
    Replies: 14
    Last Post: 12-16-2010, 11:14 PM
  3. Please answer me quickly
    By sweet girl in forum New To Java
    Replies: 20
    Last Post: 12-10-2010, 10:17 PM
  4. Help me answer a few questions
    By namlunxxx in forum XML
    Replies: 3
    Last Post: 06-16-2010, 05:26 PM
  5. Need help with Java Answer
    By alexisasoxfan in forum New To Java
    Replies: 2
    Last Post: 05-10-2010, 07:17 AM

Posting Permissions

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