| Author |
Threads
|
Alex Green
Greenhorn
Joined: Nov 02, 2004
Posts: 4
|
|
I have written a Java applet to demonstrate starting 3 threads, but when clicking on the "Start Threads Button" nothing happens. The program compiles correctly. My code is listed below. Any help would be appreciated. import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; public class Lab2_2Applet extends Applet implements Runnable { static JFrame aWindow = new JFrame("Thread Applet"); // Define threads. Thread Child1Thread; Thread Child2Thread; Thread ParentThread; //Will be T as long as threads run. boolean Trunning = true; //Label to display Child1 Message JLabel Child1Label = new JLabel(" ", JLabel.RIGHT); //Label to display Child2 Message JLabel Child2Label = new JLabel(" ", JLabel.RIGHT); //Label to display Parent Message JLabel ParentLabel = new JLabel(" ", JLabel.RIGHT); //Button to start Thread Program JButton StartThreadsButton = new JButton("Start Threads"); public void init() { Toolkit theKit = aWindow.getToolkit(); Dimension wndSize = theKit.getScreenSize(); aWindow.setBounds(wndSize.width/4, wndSize.height/4, wndSize.width/2, wndSize.height/2); FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 40, 10); Container content = aWindow.getContentPane(); content.setLayout(flow); content.add(Child1Label); content.add(Child2Label); content.add(ParentLabel); content.add(StartThreadsButton); // Create Threads. Child1Thread = new Thread(); Child2Thread = new Thread(); ParentThread = new Thread(); aWindow.setVisible(true); ActionListener al = new BListener(); StartThreadsButton.addActionListener(al); }//INIT() public void destroy() { //Thread will stop running Trunning = false; // Destroy threads Child2Thread = null; Child2Thread = null; ParentThread = null; }//END PUBLIC VOID DESTROY public void run() { while (Trunning) { // Put text textfield Child1Label.setText("I am child #1"); Child2Label.setText("I am child #2"); ParentLabel.setText("I am parent"); }//WHILE }//END PUBLIC VOID RUN class BListener implements ActionListener { public void actionPerformed(ActionEvent event) { //Start Threads Child1Thread.start(); Child2Thread.start(); ParentThread.start(); }//ACTIONPERFORMED }//BLISTENER }//CLASS Lab2_2Applet
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
What is it you would like to have happen? Have you written code to do that? This from the doc on Thread's run method:
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
Follow that hint ... look at the constructor that uses a Runnable argument. Write the code you'd like to execute on each thread and put it in a class that implements Runnable. These are pretty broad hints, hoping that you'll enjoy (and remember) filling in the details yourself. If you get stuck post some more code and we'll work from there.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Yaroslav Chinskiy
Ranch Hand
Joined: Jan 09, 2001
Posts: 147
|
|
I guess u forgot that run in a thread has to overwritten, or u must pass a runnable to a thread. just move ur run() method to some inner class that implements Runnable: class Runner implements Runnable{ String message; JLabel label Runner(JLabel label, String message){ this.message = message; this.label = label; } public void run() { while (Trunning) { // Put text textfield label.setText(message); }//WHILE }//END PUBLIC VOID RUN } Now, when u create threads, do this: Child1Thread = new Thread(new Runner(Child1Label,"I am child #1")); Child2Thread = new Thread(new Runner(Child2Label,"I am child #2")); ParentThread = new Thread(new Runner(ParentLabel,"I am parent" )); I did not test the code, but u get the idea. [ May 26, 2005: Message edited by: Yaroslav Chinskiy ]
|
 |
 |
|
|
subject: Threads
|
|
|