Package com.thread;
/**
* 1: Programmers can execute multiple threads in the program, each thread completes a function, and executes concurrently with other threads, which
* Mechanism is called multithreading
* 2: Two ways to implement threading are to inherit Java.lang.Thread or implement Java.lang.Runnable, respectively.
* Interface
* 3:thread tears when Java.lang a class in a package, objects instantiated from this class represent the thread
* 4: The code that completes the thread's true functionality is placed in the run () method, and when a tear inherits the thread class, it can
* Override the Run () method in this class, write the code that implements the thread's functionality into the run () method, and then call the thread
* The Strat () method in the class executes the thread, which is called the Run () method.
* @author Biexiansheng
*
*/
public class ThreadTest extends thread {//Specify class inherits the thread class
private int count=10;
public void Run () {//override run () method
while (true) {
System.out.print (count+ "");//Print Count variable
if (--count==0) {//makes the count variable self-decrement, and when it is reduced to 0 o'clock, exits the loop
Return
}
}
}
public static void Main (string[] args) {
New ThreadTest (). Start ();
Thread T=new threadtest ();//Two methods can be implemented threads run
T.start ();
}
}
Examples such as the following
Package com.thread;
/**
* 1: Multi-threaded instance Practice
* @author Biexiansheng
*
*/
public class ThreadTest1 extends thread{
The implementation of multithreading is implemented in the run () method.
public void Run () {
System.out.println (GetName () + "mount Stage");
int count=0;//defines a variable
while (count<=100) {
System.out.println (GetName () + "mount" +count+ "secondary Stage");
count++;
if (count%10==0) {
try {
Thread.Sleep (2000);//catch exception, hibernate 2 seconds
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}//
}
}
System.out.println (GetName () + "Go down the Stage");
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Thread t=new ThreadTest1 ();
T.setname ("Mr.thread");
T.start ();
New ThreadTest1 (). Start ();
Implementing the Runnable Interface
Thread T1=new Thread (new Runnabletest (), "ms.runnable");
T1.start ();
}
}
Class Runnabletest implements runnable{
After writing the previous sentence, the class name error, indicating the implementation of an interface, it is necessary to implement the method within this interface
@Override
public void Run () {
TODO auto-generated Method Stub
System.out.println (Thread.CurrentThread (). GetName () + "embark on stage");
The Runnable interface implementation GetName must be obtained through the thread's CurrentThread () method
int count=0;//defines a variable
while (count<=100) {
System.out.println (Thread.CurrentThread (). GetName () + "ascend" +count+ "secondary Stage");
count++;
if (count%10==0) {
try {
Thread.Sleep (2000);//catch exception, hibernate 2 seconds
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}//
}
}
System.out.println (Thread.CurrentThread (). GetName () + "Go down the Stage");
}
}
Java implementation multithreading Getting Started