• 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 Help

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ======
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
put this statement

after your while loop in the run() method and I think you will figure it out.


Barry
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.

Moving this to the Threads and Synchronization forum...
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a few problems with the code.

1. U loop sleep() while thread's status is active. Once the status changes to anything else while loop completes and run() exits. So u can not reactivate a thread.
2. if you compare Strings, u should use equals() what you r doing only works because strings are imutable. Also since they are imutable u dont need to have new String() all the time. It whouild be better if you created static int constants that hold threads state.
3. destroy() method is not implemented in java so kill action throws exception.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic