Hello There
I fairly new to
Java and trying to get to grips with it. I'm currently studying a section involving Threads. I am writing a program to control threads. The idea is as follows :
Create 10 threads. They should be in the Asleep state when created. Have a GUI to manage the threads i.e. Launch, Activate, DeActivate and Kill. When a
thread is launched, it should be in the asleep state. When a thread is activated, it should write it's number to the standard output. When the thread is deactivated, it should stop writing to the standard output. The thread should be destroyed when it's killed.
My problem is as such :
I am able to launch a thread in the asleep state. I can then activate the thread and it writes to the standard output. I then deactivate the thread and it stops writing to the standard output. However, when I try to activate a deactive thread, it does not write to the standard output.
As
testing I launch three threads. I then select the second thread and activate it. I then select the third thread and activate it as well. At this point both the second and third threads write to the standard ouput. I then deactivate the second thread. At this point the only thread writing to the standard output is the third thread. However when I activate the second thread again, it does not write to the output.
Could someone please tell me why this is happening and how I can resolve this issue. Also, how do I kill a thread. The stop method has been deprecated and also the destroy method is not implemented. I'm using Java 1.4.2.
I've included the code for the program I've written thus far.
Thanks in advance for your assistance.
Rajesh
========= CODE ========
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
public class ControlThreads extends JFrame {
private JPanel topPanel ;
private JPanel leftPanel ;
private JPanel rightPanel ;
private JPanel bottomPanel ;
private JLabel headingLabel ;
private JButton launchThread, killThread, activateThread, deactivateThread ;
private List threadList ;
private createThread[] threadsArray = new createThread[10] ;
private static int numThreads = 0 ;
Container contPane ;
public ControlThreads (
String titleText) {
super(titleText) ;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
topPanel = new JPanel() ;
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)) ;
headingLabel = new JLabel("Control Threads Program") ;
topPanel.add(headingLabel) ;
launchThread = new JButton("Launch New Thread") ;
killThread = new JButton("Kill Thread") ;
activateThread = new JButton("Activate Thread") ;
deactivateThread = new JButton("Deactivate Thread") ;
leftPanel = new JPanel() ;
leftPanel.setLayout(new GridLayout(4,1)) ;
leftPanel.add(launchThread) ;
leftPanel.add(killThread) ;
leftPanel.add(activateThread) ;
leftPanel.add(deactivateThread) ;
threadList = new List(10) ;
rightPanel = new JPanel() ;
rightPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)) ;
rightPanel.add(threadList) ;
contPane = getContentPane() ;
contPane.setLayout(new BorderLayout()) ;
contPane.add(topPanel, BorderLayout.NORTH) ;
contPane.add(leftPanel, BorderLayout.WEST) ;
contPane.add(rightPanel, BorderLayout.EAST) ;
setSize(400,300) ;
pack() ;
setVisible(true) ;
// Launch Button Handler
launchThread.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String desc = new String() ;
if (numThreads > 9) {
JOptionPane.showMessageDialog(null, "Cannot Have More Than 10 Threads", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
threadsArray[numThreads] = new createThread(numThreads) ;
desc = threadsArray[numThreads].getThreadString() ;
threadList.add(desc,numThreads) ;
numThreads = numThreads + 1 ;
}
}
} ) ;
// Kill Button Handler
killThread.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String desc = new String() ;
if (threadList.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please Select A Thread To Kill.", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
threadsArray[threadList.getSelectedIndex()].kill() ;
}
}
} ) ;
// Activate Button Handler
activateThread.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String status = new String() ;
int selectedIndex = threadList.getSelectedIndex() ;
System.out.println("selectedIndex = " + selectedIndex) ;
if (selectedIndex == -1) {
JOptionPane.showMessageDialog(null, "Please Select A Single Thread To Activate.", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
status = threadsArray[selectedIndex].getThreadStatus() ;
if (status == "Dead") {
JOptionPane.showMessageDialog(null, "Can Only Activate \"Asleep\" or \"Deactive\" Threads", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
String desc = new String() ;
System.out.println("Thread : " + threadsArray[selectedIndex].getThreadID()) ;
threadsArray[selectedIndex].setThreadStatus("Active") ;
desc = threadsArray[selectedIndex].getThreadString() ;
threadList.replaceItem(desc,threadsArray[selectedIndex].getThreadID()) ;
threadsArray[selectedIndex].start() ;
}
}
}
} ) ;
// DeActivate Button Handler
deactivateThread.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String status = new String() ;
int selectedIndex = threadList.getSelectedIndex() ;
if ( selectedIndex == -1) {
JOptionPane.showMessageDialog(null, "Please Select A Single Thread To DeActivate.", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
status = threadsArray[selectedIndex].getThreadStatus() ;
if ((status == "Dead") || (status == "Asleep")) {
JOptionPane.showMessageDialog(null, "Can Only DeActivate \"Active\" Threads", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
String desc = new String() ;
System.out.println("Thread - " + threadsArray[selectedIndex].getThreadID()) ;
threadsArray[selectedIndex].setThreadStatus("DeActivated") ;
desc = threadsArray[selectedIndex].getThreadString() ;
threadList.replaceItem(desc,threadsArray[selectedIndex].getThreadID()) ;
}
}
}
} ) ;
}
public static void main (String[] args) {
new ControlThreads("Control Threads") ;
}
class createThread extends Thread {
int threadID ;
String threadStatus ;
public createThread(int threadNum) {
this.threadID = threadNum ;
this.threadStatus = "Asleep" ;
}
public int getThreadID() {
return this.threadID ;
}
public void setThreadID(int ID) {
this.threadID = ID ;
}
public String getThreadStatus() {
return this.threadStatus ;
}
public void setThreadStatus(String status) {
this.threadStatus = status ;
}
public String getThreadString() {
String result = new String() ;
result = "THREAD " + this.getThreadID() + " : " + this.getThreadStatus() ;
return result ;
}
public void run() {
System.out.println("In run method...") ;
String desc = new String() ;
System.out.println("Thread - " + this.getThreadID()) ;
this.setThreadStatus("Active") ;
desc = this.getThreadString() ;
threadList.replaceItem(desc,this.getThreadID()) ;
while (this.getThreadStatus() == "Active") {
try {
sleep(1000);
System.out.println("Thread = " + this.getThreadID()) ;
this.wait(1000) ;
}
catch(Exception e) {
}
}
}
public void kill() {
String desc = new String() ;
this.setThreadStatus("Dead") ;
desc = this.getThreadString() ;
threadList.replaceItem(desc,this.getThreadID()) ;
this.destroy() ;
}
}
}
====== END CODE ======