Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Stop thread in java (Java)

***********************************************
COMPLETE SOURCE CODE FOR : StopThread.java
***********************************************
import javax.swing.JOptionPane;

public class StopThread implements Runnable
{
int currentValue=0;

public void run()
{
 while(true)
 {
  JOptionPane.showMessageDialog(null,"currentValue = "+currentValue);

  String temp=JOptionPane.showInputDialog(null,"CHOOSE 1 OR 2\n1-Continue Thread\n2-Stop Thread");

  if(temp.equals("2"))
  {
   JOptionPane.showMessageDialog(null,"Thread Stop, Bye");
   return;//This will make thread stop
  }
  else if(temp.equals("1"))
  {
   currentValue=currentValue+1;
   JOptionPane.showMessageDialog(null,"Please stop me, i'm tired\ncurrentValue will add one");
  }
  else
  {
   JOptionPane.showMessageDialog(null,"I don't know your input");
  }
 }
}

public static void main(String[]args)
{
 //Create a thread by create StopThread object
 StopThread a=new StopThread();

 //run thread
 a.run();
}
}


***********************************************
JUST COMPILE AND EXECUTE IT
***********************************************