Java Core Technology Volume 1 Multi-threading

Source: Internet
Author: User

<1> what is a thread

A program performs multiple tasks at the same time, each of which is called a thread (thread), which is the short name for threading control. You can run a program called multithreaded programs (nultithreaded) at the same time as a thread.

<2> What is the difference between multi-process and multi-threading

The essential difference is that each process has its own set of variables, and threads share the data. Shared variables make communication between threads more efficient and easier than communication between processes.

Thread method

    • void Interrupt ()

Sends an interrupt request to the thread. The interrupt state of the thread is set to true. If the thread is currently blocked by a sleep call, the interruptedexception exception is thrown.

    • Static Bollean interrupted ()

Tests whether the thread that is executing this command is interrupted. Note that this is a static method. This call has a side effect----It resets the interrupt state of the current thread to false.

    • Boolean ininterrupted ()

Tests whether the thread is terminated. Unlike a static interrupt method, this call does not change the interrupt state of the thread.

    • Static Thread CurrentThread ()

Returns the thread object of the current thread of execution

  <3> Status of threads (6 types)

    • New (newly created)

When a new thread is created with the ' new ' operator, such as New (R), the thread has not started running. This means that its state is new.

    • Runnable (can be run)

Once the Start method is called, the thread is in the runnable state. A running thread may or may not be running, depending on when the operating system gives the thread time to run. (A running thread may or may not be running)

    • Blocked (blocked)
    • Waiting (Wait)

When a thread is in a blocked or waiting state, it is temporarily inactive. It does not run any code and consumes the least resources. Until the thread debugger activates it again.

(1) When a thread attempts to acquire an internal object lock, and the lock is held by another thread, the thread goes into a blocking state. When all other threads release the lock, and the thread scheduler allows this thread to hold it, the thread will become a non-blocking state.

(2) When a thread waits for another thread to notify the scheduler of a condition, it enters its own wait state. This occurs when you call the Object.wait method or the Thread.Join method, or you wait for lock or condition in the Java.util.concurrent library.

    • Timed Waiting (timed Wait)

There are several methods that have a timeout parameter. Calling them causes the thread to enter a timed wait (time waiting) state. This status will be maintained until the timeout expires or the appropriate notification is received. The methods with time-out parameters are Thread.Sleep and object.wait, Thread.Join, Lock.trylock, and condition.await in a timed version.

    • Terminated (terminated)

The thread is terminated for one of the following two reasons:

(1) Natural death due to the normal exit of the Run method.

(2) Accidental death due to an uncaught exception that terminated the Run method.

Java.lang.Thread

    • void Join ()

Waits for the specified thread to terminate

    • void join (Long Millis)

Waits for the specified thread to die or passes the specified number of milliseconds.

    • Thread.state GetState ()

Get the status of this thread; one of the New, RUNNABLE, BLOCKED, waiting, timed_waiting, or terminated.

    • void Stop ()

Stop the thread. Outdated

    • void Suspend ()

Suspend execution of this thread (obsolete)

    • void Resume ()

Resumes the thread. This method is only called after calling Suspend (). Outdated

Java Core Technology Volume 1 Multi-threading

Related Article

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.