Java thread Research

Source: Internet
Author: User

1 runnable interface

public interface Runnable
 

RunnableInterfaces should be implemented by classes that intend to execute their instances through a thread. Class must be definedrun.

This interface is designed to provide a public protocol for objects that want to execute code during activity. For example,ThreadClassRunnable. Activation means that a thread has been started and has not been stopped.

In addition,RunnableNotThreadThe subclass class provides an activation method. By instantiatingThreadYou can run the instance and use it as the running target.RunnableWithout creatingThread. In most cases, if you only want to rewriterun()Instead of rewriting otherThreadMethod, then useRunnableInterface. This is important because you should not create a subclass for the class unless the programmer intends to modify or enhance the basic behavior of the class.

2 Thread class

public class Thread
Extends object
Implements runnable
 

ThreadIs the execution thread in the program. Java Virtual Machine allows applications to run multiple execution threads concurrently.

Each thread has a priority. The execution of a high-priority thread takes precedence over that of a low-priority thread. Each thread can or cannot be marked as a daemon. When the code running in a thread creates a newThreadObject, the initial priority of the new thread is set to the priority of the Creation thread, and when and only when the creation thread is a daemon thread, the new thread is the daemon.

When a Java Virtual Machine is started, there is usually a single non-daemon thread (it usually calls a specified classmainMethod ). Java virtual opportunity continues to execute the thread until any of the following situations occur:

  • CalledRuntimeClassexitAnd the security manager can exit the operation.
  • All threads of non-daemon threads have stopped running, whether returned by calling the run method or by throwing a programrunExceptions other than methods.

There are two ways to create a new execution thread. One way is to declare the classThread. This subclass should be rewrittenThreadClassrunMethod. Next, you can allocate and start instances of this subclass. For example, a thread that calculates the prime number greater than a specified value can be written as follows:

 

     class PrimeThread extends Thread {         long minPrime;         PrimeThread(long minPrime) {             this.minPrime = minPrime;         }          public void run() {             // compute primes larger than minPrime              . . .         }     } 

Then, the following code creates and starts a thread:

 

     PrimeThread p = new PrimeThread(143);     p.start(); 

Another way to create a thread is to declare the implementationRunnableInterface Class. This class is then implementedrunMethod. Then you can allocate instances of this class.ThreadIs passed and started as a parameter. The same example using this style is as follows:

 

     class PrimeRun implements Runnable {         long minPrime;         PrimeRun(long minPrime) {             this.minPrime = minPrime;         }          public void run() {             // compute primes larger than minPrime              . . .         }     } 

Then, the following code creates and starts a thread:

 

     PrimeRun p = new PrimeRun(143);     new Thread(p).start(); 

Each thread has a Identification name, and multiple threads can have the same name. If no identification name is specified during thread creation, a new name is generated.

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.