• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic