Results 1 to 7 of 7
  1. #1
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default New help with simple Thread Question

    I cant get this thread to run. I have googled the hell out of threads but I cant get it to work.. What am i doing wrong??

    it is very simple. the "Counter" class holds the "number" variable that is supposed to increment. Then a loop in the Main function prints the current number from the counter class.

    Java Code:
    package javatest;
    
    
    /**
     *
     * @author admin
     */
    public class Counter implements Runnable {
    
        int number;
        
        public Counter() {
            number = 0;
        }
        
        public void run() {
            number++;
            try {
                Thread.sleep(50);
            } catch(Exception e) { System.out.println("interupted"); }
        }
        
        public int getNumber() {
            return number;
        }
    }
    Java Code:
    package javatest;
    
    /**
     *
     * @author admin
     */
    public class Main {
        
       
        
        public Main() {
     
        }
        
        public static void main(String[] args) {
           
           Counter c = new Counter();
           Thread t = new Thread(c);
           t.start();
           
           while(true) {
               System.out.println(c.getNumber()); 
               try {
                   Thread.sleep(50);
               } catch(Exception e) { System.out.println("interupted 2"); }
           }
           
        }
    
    }
    When I run the program it displayes...

    0
    1
    1
    1
    1
    1
    1
    1
    1
    1..etc...

    what is wrong??

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    public void run() {
        number++;
        try {
            Thread.sleep(50);
        } catch(Exception e) { System.out.println("interupted"); }
    }

    So, how many times were you expecting number to be incremented?

    You might want to check out the Concurrency trail in Sun's Tutorial as it charts a way through all the perils of synchronisation etc.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    the run method is only used once; thus, number is only incremented once.

  4. #4
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    the run method is only used once; thus, number is only incremented once.
    Ya, but I thought the whole point is that the Run() method keeps running over and over until you stop the thread?? Once I activate the thread.start() it should keep running until I stop the thread. Isnt that the point of threads?

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    No sir. The point of threads is to do multiple things at the same time at the expense of memory usage.

  6. #6
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    oh ?? I See

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Should really look over Lesson: Concurrency (The Java™ Tutorials > Essential Classes) (I'm doing the same thing lol. Put it aside for now until I learn some more basic stuff)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. Thread question
    By exernet in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 10:27 AM
  2. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM
  3. Thread question
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-09-2009, 11:33 PM
  4. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:27 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
  •