Java Concurrency Base--thread class

Source: Internet
Author: User

First, the thread class constitutes the thread class to implement the Runnable interface. Part of the source code is as follows:

Second, the thread class common method 1.currentThread () method

The CurrentThread () method can return information that the code snippet is being called by, using the following:

1  Public classMyThreadImplementsrunnable{2 3 @Override4      Public voidrun () {5 System.out.println (Thread.CurrentThread (). GetName ());6     }7      Public Static voidMain (string[] args) {8         9         //New Thread (New MyThread ()). Start ();Ten         NewMyThread (). run (); One System.out.println (Thread.CurrentThread (). GetName ()); A     } -}

Result output:

Mainmain
1  Public classMyThreadImplementsrunnable{2 3 @Override4      Public voidrun () {5 System.out.println (Thread.CurrentThread (). GetName ());6     }7      Public Static voidMain (string[] args) {8         9         NewThread (NewMyThread ()). Start ();Ten         //new MyThread (). run (); One System.out.println (Thread.CurrentThread (). GetName ()); A     } -}

Result output:

Mainthread-0
2.isAlive () method

The IsAlive () method is primarily to determine whether the current thread is active (the thread has started and has not been terminated), using the following:

1  Public classMyThread2Implementsrunnable{2 3 @Override4      Public voidrun () {5System.out.println (Thread.CurrentThread (). GetName () + "is alive:" +Thread.CurrentThread (). isAlive ());6     }7      Public Static voidMain (string[] args) {8         9Thread thread =NewThread (NewMyThread2 ());Ten Thread.Start (); One System.out.println (Thread.CurrentThread (). GetName ()); ASystem.out.println ("Main is alive:" +Thread.CurrentThread (). isAlive ()); -System.out.println ("thread-0 is alive2:" +thread.isalive ());//may be false, or true if run first executes, prints false, otherwise true -          the     } -}

Result output:

Mainmain is alive: true thread-0 is alive2:truethread-0 is alive:true or Mainthread-0 is alive: true Main is alive: true thread-0 is alive2:false
3.sleep () method

The function of the sleep method is to suspend execution of the currently executing thread in a specified number of milliseconds. In particular, sleep gives the CPU the CPU to perform other tasks, but if the current thread holds a lock on an object, it does not release the lock.

1  Public classMyThread2Implementsrunnable{2 @Override3      Public voidrun () {4         5     }6      Public Static voidMain (string[] args) {7System.out.println (NewDate ());8         Try {9Thread.Sleep (3000);Ten}Catch(interruptedexception e) { One e.printstacktrace (); A         } -System.out.println (NewDate ()); -     } the  -}

Result output:

Mon Jul 21:10:27 CST 201821:10:30 CST 2018
4.yield () method

The yield () method is primarily used to allow the current thread to surrender CPU permissions and allow the CPU to execute other threads. It is similar to the sleep method and does not release the lock. However, yield does not control the specific time to hand over the CPU, and the yield method only allows threads with the same priority to have the opportunity to get CPU execution time. It is important to note that the yield method does not let the thread go into a blocking state, but instead allows the thread to return to the ready state, which only needs to wait for the CPU execution time to be fetched again, which is not the same as the sleep method.

5.join () method

If the main method executes the join method of the thread class that represents one of the threads, the main method waits for the thread thread to complete or wait for a certain amount of time. If the call is a parameterless join method, wait for the thread to finish executing, and if the join method that specified the time parameter is called, wait for a certain amount of time if the threads task is not finished yet.

1  Public classMyThread2Implementsrunnable{2 3 @Override4      Public voidrun () {5System.out.println (Thread.CurrentThread (). GetName () + "is finish");6     }7     8      Public Static voidMain (string[] args) {9Thread T =NewThread (NewMyThread2 ());Ten T.start (); One         Try { A T.join (); -}Catch(interruptedexception e) { - e.printstacktrace (); the         } -System.out.println (Thread.CurrentThread (). GetName () + "is finish"); -     } -  +}

Result output:

Thread-0 is Finishmain is finish
6.interrupt method

The interrupt method cannot break a running thread. You can interrupt a thread in a timeout wait, such as a thread in the sleep state, as follows:

1  Public classMyThread3Implementsrunnable{2 3 @Override4      Public voidrun () {5         Try {6Thread.Sleep (30000);7}Catch(interruptedexception e) {8System.out.println (Thread.CurrentThread (). GetName () + "is interrupted");9 e.printstacktrace ();Ten         } OneSystem.out.println (Thread.CurrentThread (). GetName () + "is finish"); A     } -      -      Public Static voidMain (string[] args) { theThread T =NewThread (NewMyThread3 ()); - T.start (); - t.interrupt (); -System.out.println (T.getname () + "is isinterrupted?:" +t.isinterrupted ()); +System.out.println (Thread.CurrentThread (). GetName () + "is finish"); -     } +  A}

Result output:

true Thread-0is Interruptedmain is finishjava.lang.InterruptedException:sleep interrupted    at Java.lang.Thread.sleep (Native Method) at    com.sun.lp.demo.MyThread2.run (Mythread2.java:ten)    at Java.lang.Thread.run (Thread.java:748) Thread-0 is finish
7.stop () method

is now obsolete, it is an unsafe method. Because calling the Stop method terminates the call to the Run method directly and throws a Threaddeath error. But a lot of discussion

8.getName (), SetName () and GetID ()

Gets and sets the name of the thread. Gets the ID of the thread

9.setDaemon () and Isdaemon ()

Sets the thread as the daemon thread and determines whether it is a daemon thread

Daemon thread: A special thread, with the meaning of companionship, the daemon thread is automatically destroyed when there is no non-daemon thread in the process, and the typical daemon thread is the garbage collection thread. The role of a daemon is primarily to facilitate the operation of other threads.

10.start () method

Start () is used to start a thread, and when the Start method is called, the system opens a new thread to execute the user-defined subtask, in which the appropriate thread is assigned the required resources

11.run () method

The run () method does not require a user to invoke, and when a thread is started by the Start method, when the thread obtains the CPU execution time, it enters the Run method body to perform the specific task. Note that inheriting the thread class must override the Run method and define the specific task to be performed in the Run method.

Java Concurrency Base--thread class

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.