Thread of Java (thread creation method, thread class in Java, thread synchronization, thread life cycle, communication between threads)

CPU: 10 core, main frequency 100MHz

1 core dominant frequency 3GHz

So which CPU is better?

Isn't the more CPU cores the better? Not necessarily. The dominant frequency is used to measure the processing speed of GPU. For example, is it fast for 10 cows to transport goods or fast for one aircraft to calculate goods? Obviously, it is an aircraft, so a 1-core 3GHz CPU is better. Of course, under the same dominant frequency, the more CPUs, the better.

In Java, the JVM virtual machine allows multiple threads to run through Java Lang. thread class

Thread class attribute:

Construction method:

1、 There are two ways to create threads: 1 Inherit thread class

2. Implement runnable interface

For creating threads in this way, you can name each thread, otherwise it defaults to thread num.

Implementation method: it avoids the limitation of single inheritance. Multiple threads can share the same interface to implement class objects. It is very suitable for multiple same threads to process the same resource.

2、 Related methods of thread class

(1) Basic method

Void start(): starts the thread

Run (): the name of the operation performed by the thread when it is scheduled

String getname(): returns the name of the thread

Void setname (string name): sets the name of the thread

Static currentthread(): returns the current thread

(2) Priority method

Getpriority(): get priority

Setpriority (int newpriority): sets the priority

When a thread is created, it inherits the priority of the parent thread

(3) Other methods

Static void yield(): thread yield

Join (): when the join () method of other threads is invoked in a program execution stream, the calling thread will be blocked until the join thread joins the join () method.

Static void sleep (long millis): Specifies the time in milliseconds

Stop (): force the end of the thread life cycle

Boolean isalive(): judge whether the thread is still alive

3、 Thread life cycle

Thread. Is used in JDK State to represent the state of the thread, including:

New: after declaration and instantiation;

Ready: after executing start;

Run: get the right to use the CPU, and the run () method starts running;

Blocking: the run () method stops execution and is in a waiting state;

Death: the thread completes all work or is terminated early;

4、 Thread synchronization

Question: suppose there are 4000 threads in the account, and now there are two threads, taking 2000 respectively. Since these two threads are parallel, they may be successful. At this time, the account is - 1000, which is obviously illegal. Therefore, to introduce thread synchronization, the so-called synchronization does not mean running at the same time, but co synchronization, that is, the threads are executed in sequence. In this way, when 2000 is taken out, 1000 remains in the account, and then 2000 will not be taken successfully.

Output:

Running: WeChat is running: Alipay WeChat operation account original amount: 3000 Alipay operation - account original amount: 3000 Alipay operation - withdrawal amount: 2000 Alipay operation - withdrawal after balance: 1000 operation - withdrawal amount: 2000 operation - balance after withdrawal: -1000

At this time, as we said above, withdrawal is illegal, so how to solve it? You can add a synchronization lock to the method. However, it should be noted that:

Output:

Running: WeChat is running: Alipay WeChat operation account original amount: 3000 WeChat operation - withdrawal amount: 2000 Alipay operation - account original amount: 3000 WeChat operation - withdrawal after balance: 1000 operation - withdrawal amount: 2000 operation - balance after withdrawal: -1000

Because different objects obtain different locks at this time, this method does not work, so how to change it? Just use the same object, that is:

Output at this time:

Running: WeChat WeChat operation account original amount: 3000 WeChat operation - withdrawal amount: 2000 WeChat operation - balance after withdrawal: 1000 is running: Alipay Alipay operation - account amount is insufficient: 1000

At the same time, it can further simplify:

In the last call, you only need to call account. in the run () method. Just get (), without writing the two methods separately.

After the thread that obtains the lock completes execution, it will hand over the lock to the next thread to continue execution.

For locking on static methods:

In this way, even if different objects of this class are passed in, the same lock is still obtained.

Running: WeChat WeChat operation account original amount: 3000 WeChat operation - withdrawal amount: 2000 WeChat operation - balance after withdrawal: 1000 is running: Alipay Alipay operation - account amount is insufficient: 1000

Another way is to decorate code blocks with synchronization locks:

Output:

Running: Alipay Alipay operation account original amount: 3000 Alipay operation - withdrawal amount: 2000 Alipay operation - balance after withdrawal: 1000 is running: WeChat WeChat operation - account amount is insufficient: 1000

Want different objects to have different locks:

Output:

Running: WeChat is running: Alipay WeChat operation account original amount: 3000 Alipay operation - account original amount: 3000 WeChat operation - withdrawal amount: 2000 WeChat operation - withdrawal after balance: 1000 operation - withdrawal amount: 2000 operation - balance after withdrawal: -1000

At this time, this locking method has no effect, which is the same as the first one.

In short, to make locking effective, you must obtain the lock of the same object.

5、 Communication between threads

Wait(): suspend the current thread and give up CPU and synchronize resources, so that other threads can access and modify shared resources, while the current thread queues for access to resources again.

Notify(): wakes up the higher priority thread queued for synchronization resources and ends the wait.

Notifyall(): wakes up all threads queued for resources and ends the wait.

java. The three methods provided by lang.Object can only be used in synchronized methods or synchronized code blocks.

For the last method mentioned in Section 4, as long as the same account object will be used, the locking mechanism will still succeed. But we will find that the previous result is WeChat's operation before Alipay. Suppose we want Alipay to operate first, what should we do? At this time, we need to take advantage of the communication between threads.

We first let the thread suspend for "WeChat" temporarily, we will execute other Alipay, then we will inform WeChat when the Alipay is finished.

Output:

Alipay operation account original amount: 3000 Alipay operation - withdrawal amount: 2000 Alipay operation - balance after withdrawal: 1000 WeChat operation - account amount is insufficient: 1000

At this time, we thought, suppose we are passing in different account objects?

Let's first look at the output results:

Alipay operation account original amount: 3000 Alipay operation - withdrawal amount: 2000 Alipay operation - balance after withdrawal: 1000 program has been running (this is added before notify if statement).

What does this mean? Alipay didn't tell WeChat successfully after its operation. WeChat has been waiting for the reason because they have locks of different objects, so they can't communicate between them.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>