Java learning notes thread (1), java learning notes thread

Source: Internet
Author: User

Java learning notes thread (1), java learning notes thread
Thread (1) I. Thread definition in composition:

1. Create tasks and threads:

1 TaskThread task = new TaskThread (); // create task 2 Thread taskThread = new Thread (task); // create the corresponding execution Thread Based on the task

2. Implement the Runnable interface for tasks

Override run () method

1 class TaskThread implements Runnable {2... 3 public void run () {4 // execution body, telling the system how to execute thread 5} 6} 7

3. Within the class of the execution thread, the start () method is called to execute the thread.

1 threadtask. start () // main program startup thread
Ii. Thread class

1. Method:

1 Thread () // create an empty Thread 2 Thread (task: Runnable) // create a specified task Thread 3 start (): void // start the Thread run () jvm executes 4 isAlive (): boolean // test whether the current thread is executing 5 join (): void // wait until the thread ends 6 setPriority (p: int ): void // set thread priority, 7 sleep (mills: long): void // thread sleep mills millisecond 8 yield (): void // pause the current thread and allow other threads to execute 9 interrupt (): void // interrupt the thread

2. sleep ()

Sleep () may throw a required check exception for InterruptException, and force a try/catch check exception in java.

3. thread priority

The int constants defined in Thread correspond to different priorities:

MIN_PRIORITY indicates 1;

NORM_PRIORITY indicates 5;

MAX_PRIORITY stands for 10;

Thread. NORM_PRIORITY is the main Thread priority.

The JVM always executes threads with a higher priority and then executes low ones. When the thread priority is the same, put the thread into the loop queue for cyclic scheduling.

Iii. Thread Pool

WeCreate a thread for a taskIs usedThreadClass, and we wantMultiple tasksWhen creating a thread, we need to useThread Pool

Executor InterfaceExecute tasks in the thread pool

ExecutorService is a sub-interface of Executor.

ExecutorsClass,Static MethodNewFixedThreadPool (int threadNum)Create Executor object // create a fixed number of threads in the thread pool

1 import java. util. concurrent. executorService; 2 import static java. util. concurrent. executors. newFixedThreadPool; 3 public class ExecutorDemo {4 public static void main (String [] args) {5 ExecutorService excutor = newFixedThreadPool (3); // The static method does not need a new object, call newFixedThreadPool () directly. The maximum number of threads is 3 6 excutor.exe cute (new PrintChar ('A', 100 )); // Add the first thread to the thread pool 7 excutor.exe cute (new PrintChar ('B', 100); 8 excutor.exe cute (new PrintNum (100); 9 excutor. shutdown (); // actuator off 10} 11}
 

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.