Multiple threads

Reply

Join Date: Feb 2008
Posts: 9
Reputation: adyopo is an unknown quantity at this point 
Solved Threads: 1
adyopo adyopo is offline Offline
Newbie Poster

Multiple threads

 
0
  #1
Jul 1st, 2009
Ok, so my problem is this: I did a little java program to take the number of seconds from a text box and sleep a thread for that amount of seconds. Just to make it clear, here is the piece of code I used:
  1. jTestare.addActionListener(new ActionListener() {
  2. public void actionPerformed(ActionEvent evt) {
  3. try {
  4. Thread.sleep((Integer.parseInt(getStringInterval())));
  5. } catch (InterruptedException ex) {
  6. ex.printStackTrace();
  7. }
  8. getLblReprGrafica().setOpaque(true);
  9. getLblReprGrafica().setBackground(Color.RED);
  10. getLblProcent().setText("100%");
  11. }
  12. });
Now my problem is how can I make this piece of code shared by a bunch of buttons (without them interfering with one another, like in starting one thread, moving two text boxes to the right, inputing a value then hitting Run on the respective button, and so on). I don't know if this can be done, but it was given to me as an assignment a couple of years ago and I only did it with one thread.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 53
Reputation: di2daer is an unknown quantity at this point 
Solved Threads: 12
di2daer's Avatar
di2daer di2daer is offline Offline
Junior Poster in Training

Re: Multiple threads

 
0
  #2
Jul 1st, 2009
Im not sure I understand your question, but anyway...
First of all, it looks like you're sleeping the main thread, which will cause your application to freeze for a number of seconds.
I might be off here but do you want to fire a new thread for each button but avoid write reduntant code? If so, then create a class that extends Thread and fire it with method start()

  1. public class MyThread extends Thread {
  2. public MyThread(/*argument here if needed*/) {
  3. }
  4.  
  5. public void run() {
  6. try {
  7. Thread.sleep(sleepTime);
  8. }catch(InterruptedException ie) {
  9. }
  10. // Do something here
  11. }
  12. }

fire the thread like:
  1. new MyThread().start();
---------------------------
333 - halfway to hell
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: adyopo is an unknown quantity at this point 
Solved Threads: 1
adyopo adyopo is offline Offline
Newbie Poster

Re: Multiple threads

 
0
  #3
Jul 2nd, 2009
Thanks di2daer, I'm gonna try to modify my code accordingly and see what happens.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC