Multithreading in Java

Source: Internet
Author: User

1, the main method in the thread

A) isAlive () determines whether the thread is still alive, i.e. whether the thread is not terminated

b) getpriority () Gets the priority of the thread

c) setpriority () sets the priority of the thread

d) Thread.Sleep () sets the time that the thread sleeps

e) jion () merges the current thread with the thread

f) yield () Give Cup

g) Priority of threads

I. thread.min_priority = 1 Minimum priority

II. thread.norm_priority = 5 Default Priority

Iii. thread.max_priority = 10 Maximum priority

2. Interrupt of Thread

A) Stop () is obsolete and basically does not

b) Interrupt () Simple, rough

c) It is recommended to use the SET flag bit

3. Advanced operation of Thread

A) wait () waits for the current thread until it wakes up by its thread

b) Notify () wake-up waiting thread

4, the realization of two ways of synchronization (mainly the use of synchronized)

A) lock code block

I. Synchronized (object) {block of code to lock}

b) Lock method (recommended)

I. Synchronized void method () {}

1, Java Multi-threading implementation of the main two ways, one by inheriting the thread class, one is the implementation of the Runnable interface. The main use of two methods when using multi-threading is to override the run () method to implement the code that will be executed. The second method is start (), which is used to start a thread.

2, the implementation of the thread class

1 public class Threaddemo extends thread{

2

3 public void Run () {

4 for (int i = 0; i <; i++) {

5 System.out.println (Thread.CurrentThread (). GetName () +i);

6}

7}

8 public static void Main (string[] args) {

9

Ten Threaddemo TD1 = new Threaddemo ();

Td1.setname ("Thread 1:");

12

Threaddemo TD2 = new Threaddemo ();

Td2.setname ("Thread 2:");

15

16//Get Priority

System.out.println ("Line Cheng priority is:" +td1.getpriority ());

18

19//Set the priority priority of the thread to a value of 0-10 from small to large

Td1.setpriority (min_priority);

Td2.setpriority (max_priority);

22

Td1.start ();

Td2.start ();

25

26

27}

28

29}

1, the implementation of Runnable interface

1 ublic static void Main (string[] args) {

2 ticketrunnable tr = new ticketrunnable ();

3

4 Thread TD1 = new Thread (TR);

5 td1.setname ("Window 1");

6 Thread TD2 = new Thread (TR);

7 td2.setname ("Window 2");

8 Thread td3 = new Thread (TR);

9 Td3.setname ("Window 3");

10

Td1.start ();

Td2.start ();

Td3.start ();

14}

Guangzhou Java Training Course is a combination of theory and practice, so as to ensure that students have a good grasp of theoretical knowledge at the same time, can also be based on theoretical guidance to do practical work. In order to cultivate students ' practical ability, many practical courses have been set up, in which students can experience the real process of actual project, so as to master knowledge more deeply.

Multithreading in Java

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.