Results 1 to 20 of 20
Thread: threading help
- 06-20-2010, 12:34 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
threading help
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.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(); } }
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?
- 06-20-2010, 01:04 PM #2
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.
- 06-20-2010, 01:56 PM #3
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-20-2010, 06:14 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
possible to help to edit the code directly? dun get what u all meant
-
-
Also, what folks are getting at is that the order you call things is important. You have three statements within this while loop:
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) { x++; Thread.sleep(1000); label.setText("" + x); }
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); }
- 06-20-2010, 06:33 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
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.
-
Hint (again): try changing the order of the three statements in your while loop.
- 06-21-2010, 04:24 AM #9
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
i dun get it what to change?
- 06-21-2010, 05:19 AM #10
http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-21-2010, 09:20 AM #11
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
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.
- 06-21-2010, 12:47 PM #12
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.
- 06-21-2010, 04:17 PM #13
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
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?
- 06-21-2010, 04:34 PM #14
i
what was the value of x when you clicked stop?click on stop let's say on number "2"
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?
- 06-21-2010, 05:11 PM #15
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
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.
- 06-21-2010, 05:15 PM #16
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?
- 06-21-2010, 05:21 PM #17
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
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?
- 06-21-2010, 05:27 PM #18
No. When is the label's value updated? It still has the old value until the setText() statement.is this correct?
Do you remember:Go back and add them to see what is happening and when.Add some println() statements to your code so you can see when and what is happening
- 06-21-2010, 05:47 PM #19
Member
- Join Date
- Jun 2010
- Posts
- 17
- Rep Power
- 0
its updated when i click start?
- 06-21-2010, 05:50 PM #20
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.label.setText(""+x);
Similar Threads
-
Threading
By hedonist in forum Advanced JavaReplies: 3Last Post: 03-13-2010, 02:21 PM -
need some help with threading
By dinosoep in forum New To JavaReplies: 3Last Post: 12-03-2009, 05:31 PM -
Threading
By jon80 in forum New To JavaReplies: 1Last Post: 06-13-2009, 10:53 PM -
Threading in EJB
By java08 in forum Advanced JavaReplies: 2Last Post: 08-12-2008, 11:09 AM -
Threading a method
By Sephirangel in forum Threads and SynchronizationReplies: 3Last Post: 05-05-2008, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks