[JAVA100 Example]061, realization runnable

Source: Internet
Author: User
Tags exit sleep thread

/**
* <p>title: Implements the Runnable interface to obtain a thread. </p>
* <p>description: Obtains its own thread (T2) by implementing the Runnable interface. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:twothread.java</p>
* @version 1.0
*/
public class Twothread implements Runnable {
/**
*<br> method Description: Builder. The actual thread, and start this thread.
*<br> Input parameters:
*<br> return type:
*/
Twothread () {
//get current thread
Thread T1 =thread.currentthread ( );
T1.setname ("The" the "the" the "");
System.out.println ("The Running thread:" + T1);  
//Constructs a thread by using this class (Runnable interface) and name
thread t2 = new Thread (this, "the second Thread");
System.out.println ("creat another Thread");  
//Start thread
T2.start ();
   try {
System.out.println ("I Thread'll Sleep");
Thread.Sleep (3000);  
}catch (interruptedexception e) {
System.out.println ("The" the "A-thread has wrong");

System.out.println ("A-thread exit");
}
/**
*<br> Method Description: Thread body. Implement the Run method.
*<br> Input parameters:
*<br> return type:
*/
public void Run () {
try {
for (int i=0;i<5;i++) {
System.out.println ("Sleep Time for Thread 2:" +i);
Thread.Sleep (1000);  
}
catch (Interruptedexception e) {
System.out.println ("thread has wrong");

System.out.println ("Second thread Exit");
}
/**
*<br> Method Description: Main method
*<br> input parameter:
*<br> return type:
*/
public static void main ( String args[]) {
New Twothread ();
}
}

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.