Results 1 to 20 of 20

Thread: threading help

  1. #1
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default threading help

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class fyp7   {
    	counter c = new counter();
    	JLabel label = new JLabel("");
    	JButton butt1 = new JButton("START");
    	JButton butt2 = new JButton("STOP");
    	
    	
    	public fyp7() {
    		
    		// create frame
    		JFrame frame1 = new JFrame("thread");
    		frame1.setBounds(50, 50, 400, 200);
    
    		frame1.setLayout(new BorderLayout());
    		JPanel panel1 = new JPanel();
    		JPanel panel2 = new JPanel();
    		panel1.add(label);
    		
    		panel2.setLayout(new FlowLayout());
    		
    		panel2.add(butt1);
    		panel2.add(butt2);
    		
    		butt1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				  Thread counterThread = new Thread(c);
    				  counterThread.start();
    			}
    		});
    		
    
    		butt2.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				 c.stop();
    				
    			}
    		});
    		frame1.add(panel1, BorderLayout.NORTH);
    		frame1.add(panel2, BorderLayout.SOUTH);
    
    
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setVisible(true);
    }
    
     class counter implements Runnable {
    	boolean run1;
    			public void run(){
    				 run1 = true;
    				
    				int x = 0; 
    				
    				
    				 try {
    						while (run1 ){
    							x++;
    							Thread.sleep(1000);
    							
    							label.setText(""+x);
    						}
    							
    				 
    				 }
    						 catch (InterruptedException e) {
    						 }
    		}
    		
    			
     public void stop() {
    	 run1 = false;
       }
    }
    	
    public static void main(String[] args) {
    new fyp7();
    }
    }
    i've been trying to do a threading whereby the user can click on start the number will be increasing like 1, 2 , 3 , 4 , 5 then when i press stop it will stop let's say at 3 and then if the user wants to start again it will continue from the number.

    However from my code, when i press start let's say i want it to stop at the number "2" but it doesnt stop there it will only jump till 3 then it will stop. what could be the problem here?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Looks like you need to do some debugging. Add some println() statements to your code so you can see when and what is happening. When is the flag set and when is the value incremented.

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    ...When is the flag set and when is the value incremented.
    Yep, and when Thread.sleep(...) is called.

  4. #4
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    possible to help to edit the code directly? dun get what u all meant

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by the reporter View Post
    possible to help to edit the code directly? dun get what u all meant
    Personally, I'd rather first see your code with the println statements added. This is a valuable debugging technique that you really should attempt to use as it will help you now and also in the future.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, what folks are getting at is that the order you call things is important. You have three statements within this while loop:
    Java Code:
            while (run1) {
              x++;
              Thread.sleep(1000);
              label.setText("" + x);
            }
    And if you call them in a different order, you'll see different results. Throwing in one or two System.out.println("value of x is: " + x); can help clarify this:

    Java Code:
            while (run1) {
              System.out.println("value of x at start of loop is: " + x);
              x++;
              Thread.sleep(1000);
              label.setText("" + x);
              System.out.println("value of x at end of loop is: " + x);
            }

  7. #7
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    value of x at start of loop is: 0
    value of x at end of loop is: 1
    value of x at start of loop is: 1
    value of x at end of loop is: 2
    value of x at start of loop is: 2
    value of x at end of loop is: 3
    value of x at start of loop is: 3
    value of x at end of loop is: 4
    value of x at start of loop is: 4
    value of x at end of loop is: 5

    here's the println statement when i run the start for 4 times, but somehow the problem is that when i click stop at 4 it jumps to 5 then it stop. i've tried changing the threadingsleep value but it doesnt work.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hint (again): try changing the order of the three statements in your while loop.

  9. #9
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    i dun get it what to change?

  10. #10
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Quote Originally Posted by the reporter View Post
    i dun get it what to change?
    All persons here are telling you to just change the ORDER of the statements in the while loop. I don't know which part of this statement you don't get.
    I doubt that you wrote the above code yourself !!
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  11. #11
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    i tried to change, the order but it doesnt work. it's my code i'm just asking help here i understanding the part where u all tell me which segment of my code went wrong but i dunno what to shift what? i've tried changing the thread time but it doesnt work, move the x++ it still also doesnt work.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Change the time to sleep to at least 5 seconds. Then run a test. Watch the println() output when you click on the Stop button. You should see what the value of x is and what is being shown in the label at the time that you press the button.
    Then look at the code: When do you exit the loop? When is the label updated to show the value of x?
    Its a timing thing. x is updated before you wait/sleep. The most likely time for the program to see your button press is when its sleeping.

  13. #13
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    tried changing the sleep time to at least 5 seconds, observed the output as well whenever i click on stop let's say on number "2" it will always go to "3" then it will stop. would be it be possible if u can edit my while loop and maybe i will test it out from there?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    i
    click on stop let's say on number "2"
    what was the value of x when you clicked stop?
    Then "number 2" is what is shown in the label.
    Play computer with your code. Go thru it step by step thru the loop:
    Say the label is now showing 2.
    At the top of the loop you set x to 3
    then you sleep for 5 seconds:
    .
    .
    .
    .
    .
    .
    .
    then you press stop button
    .
    ... more time passes.
    .
    Then you exit the sleep. What is showing and what is the value of x?
    Then you set the label to the value of x.
    then the loop tests if stop was pressed
    You exit the loop

    What is wrong with the timing here?

  15. #15
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    sorry i still dun get ur point. if i change the sleep time to 5, the rate of the count would be very fast and if i change to x to 3 it will just start from 3 possible u can edit my while loop? honestly speaking i dun really get what u mean but still i tried and implement it to my code.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Did you go thru the step by step I posted above?

    What is the value of x when the program exits sleep >>> Your answer here
    What is the value shown on the label when the program exits sleep >>>> Answer here
    What is the value shown on the label when the program exits the loop >>> Answer here

    If you don't want to have the value on the label changed after you press Stop,
    how can you change your code so it won't change after Stop is pressed?

  17. #17
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    What is the value of x when the program exits sleep >>> 6
    What is the value shown on the label when the program exits sleep >>>> 6
    What is the value shown on the label when the program exits the loop >>> 6

    is this correct?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    is this correct?
    No. When is the label's value updated? It still has the old value until the setText() statement.

    Do you remember:
    Add some println() statements to your code so you can see when and what is happening
    Go back and add them to see what is happening and when.

  19. #19
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    its updated when i click start?

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    label.setText(""+x);
    No, its updated by the above statement which is in a method that is in a thread that is started when you click Start. There are steps and steps and steps taken by the computer to get to the point where the label is updated. You have to be able to think about what happens step by step to understand what the computer is doing so you can make it do what you want.

Similar Threads

  1. Threading
    By hedonist in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2010, 03:21 PM
  2. need some help with threading
    By dinosoep in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 06:31 PM
  3. Threading
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 11:53 PM
  4. Threading in EJB
    By java08 in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 12:09 PM
  5. Threading a method
    By Sephirangel in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-05-2008, 08:38 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
  •