Java multithreaded Walkthrough

Source: Internet
Author: User
Tags thread class

Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can be run at the same time. Each part of such a program is called a thread, and each thread defines a separate execution path.

Multithreading is a special form of multitasking. Multithreading requires less overhead than multitasking processing.

You need to define other terminology Related: threads and processes . A process includes the operating system, which can contain one or more threads that allocate memory space. A thread cannot exist alone, it must be part of a process. A process is still running until all non-daemon threads are executed.

Multithreading is able to write very efficient programs to maximize CPU use because idle time can be kept to a minimum.

The life cycle of a thread:

A thread goes through different stages of its life cycle. For example, a thread produces, starts, runs, and then dies. Shows the entire life cycle of a thread.

The above phases are explained:

    • New : A fresh thread begins the life cycle of the new state. It is still in this state until the program starts the thread. It is also known as the birth thread.

    • Runnable: When a new thread is started, the thread enters the operational state. Threads in this state are considered to perform their tasks.

    • Waiting: Sometimes a thread transitions to a wait state while the thread waits for another thread to perform the task. One thread transitions back to the operational state, and only the waiting thread that signaled the other thread continues to execute.

    • Timed waiting: A thread that can be run can enter a timed wait state at a specified time interval. When a thread in this state transitions back to a running state, the time interval expires or the wait event occurs.

    • Terminated: A running thread enters the terminating state when it finishes its own task, otherwise terminates.

Priority of the thread:

Each Java thread has a priority that can help the operating system determine which thread is scheduled in the order.

Java precedence is within the range of min_priority (constant 1) and max_priority (constant 10). By default, each thread takes precedence over Norm_priority (constant 5).

A thread with a higher priority is more important than a program and a low-priority thread should be allocated before processor time. However, thread precedence does not guarantee which thread executes the order and is very dependent on the platform.

To create a thread:

There are two ways in which the Java definition can be done:

    • The Runnable interface can be implemented.

    • You can extend the thread class itself.

To create a thread by implementing runnable:

The simplest way to create a thread is to create a class that implements the Runnable interface.

In order to implement runnable, this class needs to implement only a single method of Run (), which is declared like this:

Publicvoid run()   

Defines the internal code that forms the run () method of the new thread. It is important to understand that run () can call other methods, use other classes, and declare variables, just as the main thread can be important.

When you create an implementation of the Runnable class, the thread's object is instantiated from the class. A thread defines multiple constructors. We will use one of the following as follows:

StartingHello thread...StartingGoodbye thread...HelloHelloHelloHelloHellohellogoodbyegoodbyegoodbyegoodbyegoodbye.......
Main Threading Concepts:

Multithreaded programming, it is necessary to have the following concepts:

    • Thread synchronization

    • Inter-thread communication

    • Thread deadlock

    • Line programmed: Pause, stop and resume

Using Multithreading:

The key to effective use of multithreaded support is at the same time, not serially. For example, when you have a program, you can execute the two subsystems concurrently so that they run on each thread.

As long as you use multiple threads carefully, you can create very efficient programs. Note that if you create too many threads, you can actually reduce the performance of the program rather than improve it.

Keep in mind that some of the overhead is related to context switching. If you create too many threads, more CPU time will be spent in a constantly changing environment than executing programs!


Java multithreaded Walkthrough

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.