Results 1 to 10 of 10
  1. #1
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default threads question

    I am trying to learn how to use threads to run a couple of methods repeatedly. This is my code and I get a compilation error when I try to run it. I am confused. help.
    Java Code:
     public void doA() {
            System.out.println("now I am doing A");
        }
        public void doB() {
            System.out.println("now I am doing B");
        }
        public void sleep() {
            System.out.println( " Zzzzzzz...");
        }
    
        public void live() {
            new Thread() {
                public void run() {
                    while (true) {
                        doA();
                        doB();
                        sleep();
                    }
                }
            }.start();
        }

  2. #2
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    Threads are hard. I think that the sleep method that this anonymous class is using is its sleep() method which needs a parameter passed in. I do not think it is using the sleep method you created. I changed your method name to doze() and changed it in the anonymous class and it ran , but of course it never stopped running. Somewhere you need to give it a time to stop, unless of course it is something you want to run forever, which, I supposed, you might

  3. #3
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    You could also use the sleep(long) method instead of yours.
    Java Code:
    long interim=1000;
    public void live() {
    		
    	new Thread()
    	{
    		public void run()
    		{
    		while (true)
    		{
    			doA();
    			doB();
    		try
    		{
    			sleep(interim);
    		} catch (InterruptedException e)
    		{
    			e.printStackTrace();
    		}
    		}
    		}
             }.start();
    		
        }

  4. #4
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    Thanks to both of you.

  5. #5
    Maris is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default

    can anyone help me?I have one question i.e what is the difference between start() and run.we can run a thread by thread.run() then why we need start().

  6. #6
    ingle.anil is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default sequential execution

    thread.start() -- concurrent execution
    1)create new thread and
    2)then calls run() method.

    thread.run() -- sequential execution
    1)calls run() method.
    - no new Thread creation.

  7. #7
    rorootot is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    One more question.
    Why in front of start () is put dot, like that ''.start()''?
    Does in threads is obligatory to do like that?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    That is the syntax for calling an object's method: <object-ref>.<method>...

  9. #9
    lagnajita is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Smile Producer Consumer

    Hello All,

    I am new to this community as well as Java.

    I need to understand how threading works in Java and have to imolement the code for hungry birds problem and The Bear and the Honeybees problem in Java using semaphore.

    help needed.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    You should open a new post.... this post (the one you used) is very old (almost two years). As for your question about threads and semaphores, here's a couple of links:

    Processes and Threads (The Java™ Tutorials > Essential Classes > Concurrency)

    Semaphore (Java 2 Platform SE 5.0)

    Programming Examples: Java Semaphore

    Example Java classes

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. applets & threads
    By willemjav in forum Java Applets
    Replies: 2
    Last Post: 04-04-2008, 06:59 AM
  3. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM
  4. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 PM
  5. Server n threads
    By ferosh in forum Networking
    Replies: 2
    Last Post: 04-28-2007, 10:42 PM

Posting Permissions

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