The 1th chapter Java multithreading skills

Source: Internet
Author: User

Concept of the process:

Process is the basis of the structure of the operating system, is the execution of a program, is a program and its data in the processing machine sequential execution of the activity, is the program on a data set running process, it is the system for resource allocation and scheduling of an independent unit.

Thread Concept:

Threads can be interpreted as subtasks that run independently in the process. For example, QQ.EXE can have video threads, threads to download files, threads to send emoticons, threads to transfer data, and so on.

Ways to implement Multithreading:

1. Inheriting the thread class
2. Implement the Runnable interface

Inherit the thread class

The 1.Thread class implements the Runnable interface, and the thread class is structured as follows:
public class Thread implements Runnable
2. Cons: Java is single-inheritance, so inheriting the thread class can no longer inherit other interfaces
Threads created in these two ways are the same in nature at work, with no essential difference.
The start () method in the 3.Thread class notifies the thread planner that this thread is ready to wait for the calling thread's run () method.
4. The order in which the start () method is executed does not represent the order in which threads are started

instance variables and thread safety

Instance variables in a custom thread class can have shared and unshared points for other threads.

(1) Non-sharing of data
public class MyThread1 extends Thread {        private int count = 5;
public MyThread1(String name) { super(name); this.setName(name); }
@Override public void run() { super.run(); while (count > 0) { count--; System.out.println("由" + this.currentThread().getName() + "计算,count=" + count); } } }
(2) Information on shared data

Sharing data is where multiple threads can access the same variable, such as when the software that implements the voting function, multiple threads can simultaneously process the same person's votes.

public class MyThread1 extends Thread {    private int count = 5;
public MyThread1(String name) { super(name); this.setName(name); }
@Override public void run() { super.run(); count--; System.out.println("由" + this.currentThread().getName() + "计算,count=" + count); }}
Synchronized can be locked on arbitrary objects and methods, while the code that is locked is called a "mutex" or a "critical section". When a thread wants to execute the code inside the synchronization method, the thread first tries to get the lock, and if it can get the lock, the thread can execute the code inside the synchronized. If you can't get the lock, the thread will keep trying to get the lock until it gets there, and there are multiple threads competing for the lock.
The concept of non-thread safety

Non-thread-safe mainly refers to the situation that when multiple threads operate on the same instance variable in the same object, the value is changed and the value is not synchronized, which affects the program execution process.

Stopping a thread

There are 3 ways to terminate a running thread in Java:
1) Use the exit ID to cause the thread to exit normally, that is, when the Run method finishes.
2) Use the Stop method to forcibly terminate the thread, but this method is not recommended because stop and suspend and resume are obsolete methods.
3) Use the interrupt method to break the thread. Recommended usage.

Calling the interrupt () method simply marks a stop in the current thread and does not really stop the thread.

To determine if a thread is a stopped state

In the Java SDK, there are two methods available in the Thread.java class.
1) this.interrupted (): Test if the front thread has been interrupted. After execution, there is a feature that clears the status identifier to False.
2) this.isinterrupted (): Tests if thread object has been interrupted. However, the status identification is unclear.
Difference:
Explanation of the This.interrupted () method: Tests whether the current thread is a thread that runs the this.interrupted () method when the front-line is interrupted. The interrupt state of the thread is purged by this method. In other words, if the method is called twice in a row, the second call will return False (except in the case where the current thread is interrupted again until the first call has cleared its break state and the second call has finished verifying the break state).

The thread that can stop--exception method

Call the interrupt () method to break the thread, but the subsequent method will continue to run. You can stop running the following method by throwing an exception.

Stop in the sleep

If a thread is stopped in the sleep state, it goes into the catch statement and clears the stop state value so that it becomes false.

Bad consequences of releasing locks

Using Stop () to release a lock will result in inconsistencies in the data. Another situation is to make some cleanup work less than complete.

Pausing a thread

Pausing a thread means that the thread can also resume running. In Java Multi-threading, you can use the Suspend () method to pause a thread and resume the execution of a thread using the resume () method. Disadvantages of the Suspend and resume method: 1. Exclusive 2. Out of sync

Daemon Threads

There are two kinds of threads in Java, one is a user thread and the other is a daemon thread.
A daemon thread is a special kind of thread that has a special "companion" meaning, and the daemon thread automatically destroys when there is no non-daemon thread in the process. A typical daemon thread is a garbage collection thread, and when there is no non-daemon thread in the process, the garbage collection thread is not necessary to automatically destroy it. In Java, the daemon thread is set through Setdaemon ().

The 1th chapter Java multithreading skills

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.