Results 1 to 1 of 1
  1. #1
    atch is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Default Do I have to put Thread to sleep?

    Hi guys, I have two classes A and B.
    Java Code:
    import java.util.Random;
    
    
    public class Commander_1 extends Thread
    {
    
        private final String[] QUESTIONS =
        {
            "How old are you?",
            "How many fitness tests have you taken?",
            "Are you prepared for the war?"
        };
        private int nextQuestion = 0;
        private boolean waitingForAnswer = false;
    
        private synchronized void setWaitingForAnswer(boolean value)
        {
            waitingForAnswer = value;
        }
    
        public synchronized boolean isWaitingForAnswer()
        {
            return waitingForAnswer;
        }
    
        public synchronized void sir()
        {
            waitingForAnswer = false;
        }
    
        private int setNextQuestion()
        {
            return new Random().nextInt(3);
        }
    
        private synchronized void askQuestion()
        {
            while (isWaitingForAnswer())
            {
                try
                {
                    wait();
                }
                catch (InterruptedException e)
                {   }
            }
            nextQuestion = setNextQuestion();
            System.out.println("Commander (Q" + (nextQuestion + 1) + ") " + QUESTIONS[nextQuestion]);
            setWaitingForAnswer(true);
            notifyAll();
    
        }
    
        public synchronized int getNextQuestion()
        {
            return nextQuestion;
        }
    
        @Override
        public void run()
        {
            for (int i = 0; i < 3; ++i)
            {
                askQuestion();
                try //WITHOUT THIS TRY BLOCK CODE WONT WORK. WHY?
                {
                Thread.sleep(500);
                }
                catch(InterruptedException e)
                {
    
                }
            }
        }
    }
    
    
    public class Soldier_1 implements Runnable
    {
    
        private final String[] ANSWERS =
        {
            "25",
            "2",
            "All of us are sir!"
        };
        private int nextAnswer = 0;
        private Commander_1 myCommander;
    
        Soldier_1(Commander_1 commander)
        {
            myCommander = commander;
        }
    
        private synchronized void answerQuestion()
        {
            while (!myCommander.isWaitingForAnswer())
            {
                try
                {
                    wait();
                }
                catch (InterruptedException e)
                {
                }
            }
            if (myCommander.isWaitingForAnswer())
            {
            myCommander.sir();
            nextAnswer = myCommander.getNextQuestion();
            System.out.println("Soldier (A" + (nextAnswer + 1) + ") " + ANSWERS[nextAnswer]);
            notifyAll();
            }
        }
    
        @Override
        public void run()
        {
            for (int i = 0; i < 3; ++i)
            {
                answerQuestion();
                 try //WITHOUT THIS TRY BLOCK CODE WONT WORK. WHY?
                {
                Thread.sleep(500);//IF I CHANGE THIS VALUE TO BE DIFFERENT 
                }//TO THE VALUE IN PREV. CLASS IT ALSO FREEZES.
                catch(InterruptedException e)
                {
    
                }
            }
        }
    }
    Thanks for any help with this problem.
    Last edited by atch; 03-08-2010 at 11:00 AM.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM
  3. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

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
  •