What is multithreading? These words will not be said, look directly at examples.
first, the use of multi-threading1. Inherit the thread class
Package Com.ztz.mythread;public class MyThread extends thread{@Overridepublic void Run () {System.out.println ("Inherit Thread ");} public static void Main (string[] args) throws Exception {MyThread t=new MyThread (); T.start ();}}
2. Implement Runnable interface
Package Com.ztz.mythread;public class Myrunnable implements runnable{@Overridepublic void Run () {System.out.println (" Implement the Runnable Interface ");} public static void Main (string[] args) throws Exception {myrunnable runnable=new myrunnable (); Thread thread=new thread (runnable); Thread.Start ();}}
PS: The start of a thread is the start () method, not the run () method. The way to inherit the thread class is limited, because Java is a single inheritance, in order to change this limitation, we can implement the Runnable interface to implement multithreading
Second, CurrentThread () methodthe CurrentThread () method returns information about which thread the code snippet is being called from.
Package Com.ztz.mythread;public class Mythread{public static void Main (string[] args) throws Exception { System.out.println (Thread.CurrentThread (). GetName ());}}
Run the method, console output:Main
Description: The Main method is called by the name main thread. Thread.CurrentThread (). GetName () is the Get thread name
Package Com.ztz.mythread;public class MyThread extends Thread{public MyThread () {System.out.println ("Construction Method:" + Thread.CurrentThread (). GetName ());} @Overridepublic void Run () {System.out.println ("Run:" +thread.currentthread (). GetName ());} public static void Main (string[] args) throws Exception {MyThread thread=new MyThread (); Thread.Start ();}}
Run the method console output:Construction method: Main
run:thread-0
in fact, main is the main thread Thread-0 is a child thread, if the start of a thread is Thread-1.
third, Sleep () methodthe function of the sleep () method is to let the currently executing thread hibernate (suspend execution) within the specified number of milliseconds, and the executing thread is the thread returned by This.currentthread ().
Package Com.ztz.mythread;public class MyThread extends thread{@Overridepublic void Run () {try{system.out.println ("Run Start: "+system.currenttimemillis ()); Thread.Sleep (2000); System.out.println ("Run End:" +system.currenttimemillis ());} catch (Exception e) {}}public static void main (string[] args) throws Exception {MyThread thread=new MyThread (); SYSTEM.OUT.PRINTLN ("Thread Start:" +system.currenttimemillis ()); Thread.Start (); SYSTEM.OUT.PRINTLN ("Thread End:" +system.currenttimemillis ());}}
Output Result:Thread start:1437233204468
Thread end:1437233204468
Run start:1437233204468
Run end:1437233206468
Iv. getId () methodthe function of the GetId () method is to obtain a unique identifier for the thread.
Package Com.ztz.mythread;public class Mythread{public static void Main (string[] args) throws Exception {Thread CurrentThread = Thread.CurrentThread (); System.out.println (Currentthread.getname () + "--->" +currentthread.getid ());}}
Output Result:Main--->1
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
On Java multithreading