/*** Thread: is an execution path of the process, sharing a memory space, the thread can freely switch between, concurrent execution, a process at least one process (single-threaded) * Multithreading two implementations: 1. Inherit the thread Class 2. Implementing the Runnable Interface*/ Public classMain { Public Static voidMain (string[] args) {MyThread T1=NewMyThread (); T1.start (); //Start ThreadSystem.out.println ("main"); System.out.println ("Finished."); Myrunnable Ta=Newmyrunnable (); Thread T2=NewThread (TA); T2.start (); }} classMyThreadextendsthread{@Override Public voidrun () { for(inti=0;i<10;++i) {System.out.println (NewDate () + "-" +i); } }}classMyrunnableImplementsrunnable{//It is recommended to use this (can inherit other classes, the first one inherits the thread can not inherit the other)@Override Public voidrun () { for(inti=0;i<10;++i) {System.out.println ("Myrunnable" + "-" +i); } }}
Public classMain { Public Static voidMain (string[] args) {System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName ());//Output MainThread T1 =NewThread (NewMyrunnable (), "Fish7");//System.out.println (T1.getid ());T1.setname ("Dining"); System.out.println (is the T1 thread active? "+t1.isalive ()); T1.start (); System.out.println (is the T1 thread active? "+t1.isalive ()); }}classMyrunnableImplementsrunnable{//It is recommended to use this (can inherit other classes, the first one inherits the thread can not inherit the other)@Override Public voidrun () {System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName ());//Output Main }}
Public classMain {/*** Sleep method principle: Let the current thread into hibernation, give up the CPU time of the execution, but do not release the ownership of the monitor*/ Public Static voidMain (string[] args) {Thread T1=NewThread (Newmyrunnable ()); T1.start (); for(inti=0;i<10;++i) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); Thread.Sleep (1000);//do not lose ownership of any monitor}Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}classMyrunnableImplementsrunnable{//It is recommended to use this (can inherit other classes, the first one inherits the thread can not inherit the other)@Override Public voidrun () { for(inti=0;i<10;++i) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}
Public classMain {/*** Join method: Waits for the current thread to execute a termination, or a specified wait time (milliseconds, nanoseconds)*/ Public Static voidMain (string[] args) {Thread T1=NewThread (Newmyrunnable ()); T1.start (); for(inti=0;i<10;++i) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); if(i==5) T1.join (); //wait for the thread to endThread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}classMyrunnableImplementsrunnable{//It is recommended to use this (can inherit other classes, the first one inherits the thread can not inherit the other)@Override Public voidrun () { for(inti=0;i<10;++i) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}
Public classMain {/*** Interrup method: Just set the interrupt state of the thread to true and do not really interrupt the thread. */ Public Static voidMain (string[] args) {Thread T1=NewThread (Newmyrunnable ()); T1.start (); for(inti=0;i<10;++i) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); if(i==5) T1.interrupt (); //interrupt thread, set a break token (the interrupt status is true)Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}classMyrunnableImplementsrunnable{@Override Public voidrun () {inti = 0; while(!thread.interrupted ()) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); ++i; Thread.Sleep (1000);//throws an exception if the interrupt is marked as true}Catch(Interruptedexception ex) {ex.printstacktrace (); Thread.CurrentThread (). interrupt (); } } }}
Public classMain {/*** Custom mark completion in the thread * Interrupted program can only be interrupted by itself, others must not interrupt it. */ Public Static voidMain (string[] args) {myrunnable my=Newmyrunnable (); Thread T1=NewThread (my); T1.setpriority (thread.max_priority); //set the thread'sThread.CurrentThread (). setpriority (thread.min_priority); T1.start (); for(inti=0;i<10;++i) { Try{System.out.println ("Currently running thread Name:" +thread.currentthread (). GetName () + "-" +i); if(i==5) {My.setflag (false);//break thread in} thread.sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}classMyrunnableImplementsrunnable{Private BooleanFlag =true; Public BooleanIsflag () {returnFlag; } Public voidSetflag (Booleanflag) { This. Flag =Flag; } @Override Public voidrun () {inti = 0; while(flag) {System.out.println (Thread.CurrentThread (). GetName ()+"-"+i); I++; Try{Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}
Public classMain {/*** Daemon Thread: All daemons will terminate automatically when there is no user thread in the program! (daemon user thread) * Setdaemon: marks the thread as either a daemon thread or a user thread. When a running thread is a daemon thread, the Java virtual machine exits. * The method must be called before the thread is started. * Yield: Pauses the currently executing thread object and executes other threads. */ Public Static voidMain (string[] args) {//The main thread is the user threadMyrunnable my =Newmyrunnable (); Thread T1=NewThread (my); T1.setdaemon (true);//set the thread as the daemon threadSystem.out.println (T1.isdaemon ()); T1.start (); for(inti=0;i<10;++i) {System.out.println ("main-" +i); if(i==5) {Thread.yield ();//Let out the execution time of the second CPU } Try{Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}classMyrunnableImplementsrunnable{@Override Public voidrun () { for(inti=0;i<20;++i) {System.out.println ("Myrunnablei" +i); Try{Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } } }}
Java Note 13__ Create thread/thread hibernate/wait thread termination/thread break/daemon Thread