[JAVA100 case]066, line program control system

Source: Internet
Author: User

/**
* <p>title: Thread control </p>
* <p>description: Controls on threads, interrupts, hangs, restores, stops </p>
* <p>copyright:copyright (c) 2003</p>
* <P>FILENAME:THREADCTRL.JAVA</P>
* @version 1.0<  br> */
public class threadctrl{
public static void Main (String [] main) {
New Threadctrl ()

/**
*<br> Method Description: constructor, control other threads
*<br> input parameters:
*<br> return type:
*/
Threadctrl () {
try{< br> Thread TM = Thread.CurrentThread ();
Threadchild td = New Threadchild ();
Td.start ();
Tm.sleep (500);
System.out.println ("Interrupt Child thread");
Td.interrupt ();

System.out.println ("Let Child Thread wait!");
//td.wait ();
//td.suspend ();
Tm.sleep (1000);   

System.out.println ("Let Child thread Working");
Td.fauxresume ();
//td.resume ();
Tm.sleep (1000);
Td.runflag = false;
System.out.println ("main over .... ..."), and;
  }catch (Interruptedexception IE) {
System.out.println ("Inter main::" +ie);
} catch (Exception e) {
System.out.println ("Main::" +e);
}
}

}
/**
*<br> class Description: Controlled Thread class
*/
Class Threadchild extends Thread {
Boolean runflag = True
Boolean suspended = true;    
Threadchild () {
}
Public synchronized void Fauxresume () {
suspended = false;
Notify ();     
}
public void Run () {
while (runflag) {
System.out.println ("I am working ...");
    try{
Sleep (1000);
}catch (interruptedexception e) {
System.out.println ("Sleep::" +e);
}     
Synchronized (this) {
try{
if (suspended)
Wait ();
}catch (interruptedexception e) {
System.out.println ("Wait::" +e);
}
}
}
System.out.println ("Thread over ...");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.