Multithreading in Java

Source: Internet
Author: User
Multithreading in Java-general Linux technology-Linux programming and kernel information. The following is a detailed description. JAVA's multithreading feature is a major advantage. Multithreading is relative to a single thread. A single thread means that only one program is running at any time, and other programs must wait. With multithreading, JAVA supports concurrent execution of multiple programs. When you want to write a program that can execute multiple functions at the same time, you need to use the JAVA multithreading function. JAVA's multithreading function is encapsulated in the Thread class. This section describes how to use a thread.

I. Thread Creation

There are two ways to create a thread. The first method is to create a Thread class by inheriting the class "Thread. For example:

Class aaa extends Thread {

// Aaa is the name of the class

Public void run (){

// Run is the entrance to the entire thread-class code

// Similar to main in C

// Source program

}

}

The second method is to create a class with the Runnable interface. For example:

Class aaa implements Runnable {

// Aaa is the name of the class

Public void run (){

// Run is the entrance to the entire thread-class code

// Similar to main in C

// Source program

}

}

Ii. Thread calls

If the first method is used, the calling format of the created Thread class is as follows:

Aaa test = new aaa ();

// Test is an instance of the thread class aaa

Test. start ();

// Start is a member function of the thread class.

// A new thread, which automatically calls run

If the second method is used, the calling format of the created Thread class is as follows:

Aaa test = new aaa ();

// Test is an instance of the aaa class.

New Thread (test). start ();

// Create through Thread

// A new thread

Iii. Variables and member functions of the thread class

Variable:

MAX_PRIORITY specifies the maximum value that can be set for thread priority.
MIN_PRIORITY specifies the minimum value that can be set for the thread priority.
Default Value of NORM_PRIORITY thread priority:
ActiveCount () returns the number of currently activated threads in the thread group.
CheckAccess () checks whether the current thread can be modified
CountStackFrames () returns the number of stack boxes for this thread
CurrentThread () returns the currently executing thread object
Destroy () terminates a thread and does not clear other related content
DumpStack () is a debugging process that outputs the stack usage of the current thread.
Enumerate (Thread []) copies all activated threads in this Thread group to a special array.
GetName () indicates the name of the returned thread.
GetPriority () returns the priority of the thread
GetTheradGroup () returns the thread group
Interrupt () sends an interrupt message to a thread.
Interrupted () checks whether the thread is interrupted
IsAlive () checks whether the thread is active
IsDaemon () checks whether the thread is resident in memory
IsInterrupted () checks whether another thread is interrupted
Join (long) waits for the thread to stop in long milliseconds
Join (long, int) waits for the thread to stop within long milliseconds int nanoseconds
Join () always waits for the thread to stop
Resume () re-start executing this thread
Run () the entire code entry is similar to main in C.
SetDaemon () sets this thread to Daemon (resident memory)
SetName (String) sets the thread name
SetPriority (int) sets the thread priority
Sleep (long) sleep a thread for long milliseconds
Sleep (long, int) enables a thread to sleep for long milliseconds int nanoseconds
Start ()

Start a thread, which will automatically call the run function. At the same time, when a new thread starts to execute, the thread that calls start will immediately return the execution main program stop () to terminate a thread.

Stop (Throwable) terminates a thread inherited by the Throwable class.
Suspend () pause thread execution
ToString () returns a string, including the thread name, priority, and thread group.
Yield () causes the thread to discard CPU control and run other threads in the waiting state.
  

Iv. Synchronization settings

When two functions need to use the same resource, they cannot be executed simultaneously. JAVA provides the synchronization setting function. Each class can be set to synchronize the parts in its own code that cannot be run with other programs at the same time. The synchronization setting function notifies JAVA which code segments have been locked and can only be run independently. You can set synchronization in either of the following ways:

1. Set the function to synchronous

Public synchronized void fun1 (){

// Source code

}

Public synchronized void fun2 (){

// Source code

}

For functions set as synchronous, the function can only run after obtaining resources. Each class has only one resource. At any time, only one program Obtains resources is running, and the function is waiting before obtaining resources.

2. It is indicated in the call function that the function needs to be synchronized.

Synchronized (res ){

Res. fun1 ();

}

Synchronized (res ){

Res. fun2 ();

}

In the above example, fun1 and fun2 can only run after obtaining res resources. After the function is called, the resource is released, so that other threads waiting for the resource can run.

V. Thread variable security

You can set the variables in the thread to secure. This will prevent other threads from changing the value of this variable when the thread uses this variable. This function makes code optimization easier with the compiler. If this setting is not used, it is difficult for the compiler to distinguish the modified variables in a thread. The security variables are defined as follows:

Threadsafe int ttt;

// Threadsafe is the thread security variable type, and ttt is the variable name

Common static variables are typical examples of thread security variables.
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.