Java Multithreading Basics (ii)

Source: Internet
Author: User

Basic concepts

Process: The process of executing a program. Holds resources (shared memory, shared files), and threads.

Thread:

-is a sequential control flow within a program.

-it is contained in the process and is the smallest execution unit in the system, and the same process can have multiple threads.

-the thread shares the resources of the process.

Thread and process comparison:

-Each process has a separate code and data space (process context), and the process switching overhead is high.

-Threads: A lightweight process that shares code and data spaces with a class of threads, each with a separate run stack and program counter (PC), with minimal thread switching overhead.

Multi-process: In the operating system, you can run multiple tasks (programs) at the same time.

Multithreading: In the same application, there are multiple sequential streams executing simultaneously.

Interaction of Threads:

Creating threads from the thread class

Thread class

Directly inherits the object class and implements the Runnable interface. Located in the Java.lang package.

Encapsulates the properties and methods required for a thread object

Inherit the thread class--one of the ways to create multithreading

-Inherits the thread class (derives a subclass from the thread class and creates the object of the subclass)

-subclasses should rewrite the thread class's Run method to write the statement segments that need to be executed in the new thread

-Call the Start method to start a new thread and automatically enter the Run method

Execution Result:

Main thread Open

Main thread End

New Thread Open

10:3.6288 million

End of New thread

Result Description:

(1) After the main thread has been executed, the new thread will start executing

(2) The main method calls the Thread.Start () method to start a new thread and does not wait for its run method to return to continue running, the thread's Run method runs on its own side, without affecting the original main method of running

The sleep of a thread

The reason for thread hibernation is to let other threads get the chance to execute.

If you start a new thread and expect the main thread to last more than once, add a statement that lets the current thread (which is, of course, main) sleep for 1 milliseconds after the start statement.

Execution Result:

Main thread Open

New Thread Open

10:3.6288 million

End of New thread

Main thread End

Case 2: Turn on three threads, each thread sleeps 0~6 milliseconds

Execution Result:

Open Thread

Main thread End

Thread1 ready to sleep: 2081

Thread3 ready to sleep: 5298

Thread2 ready to sleep: 2812

Thread1 Sleep End

Thread2 Sleep End

Thread3 Sleep End

Description: Since thread 3 sleeps for the longest time, the last end, thread 1 sleep time is the shortest, so the first end.

Learning detection

1. One of the mistakes in the following statement is

A thread is an instance of a thread class

The thread starts execution from the runnable instance run () method passed to the purebred

The data from the thread operation is from the runnable instance

The new thread calls the start () method to immediately enter the running state the new thread calls the start () method to immediately enter the running state-correct

2, the following about the thread class provides a method of threading control, one of the errors is

Thread A is executing the join () method in threads B, and thread A waits until B executes the completion

Thread A interrupts its blocking state by calling the interrupt () method

If thread A calls method IsAlive () returns a value of true, then A is executing

The CurrentThread () method returns a reference to the current thread-the CurrentThread () method returns a reference to the current threads-correct

3, the following about the thread of the argument to, the right one is

Multiple threads created by the same class run at the same time end

Create a thread only by inheriting the Java.lang.Thread class

A thread that is paused by calling the Suspend () method will no longer be re-executed

The execution of the program finishes with the Hyper-threading (Daemon threads) Independent program finishes executing with the Hyper-threading (daemon threads)-Correct

Thread class Common API

Java Multithreading Basics (ii)

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.