Reproduced in: http://www.itxuexiwang.com/plus/view.php?aid=148
A thread is a task that can be executed in parallel in a program.
The Java runtime always executes by selecting the highest-priority thread that is currently in the ready state. If several ready threads have the same priority, the time slice method will be used to take turns to allocate the processor.
The difference between a process and a thread
The process belongs to a separate running program, and the thread is a branch inside the program. Many threads make up the run of a program.
Two ways to create a thread
(1) Thread classes are represented by the thread class and its subclasses, and the inherited run () method defines the task body when the thread executes, defining a thread class that inherits from thread to override the run () method.
(2) Any object that implements the interface runnable can be the target object of a thread, and the class thread itself implements the interface runnable, and the Run () method in the interface needs to implement the task body for the thread execution.
(3) The thread's class method CurrentThread () can be used to obtain information about the current execution threads.
Method One:
public class Person extends thread{
Public person (String name) {
This.name=name;
}
@Override
public void Run () {
Super.run ();
}
}
1
2
3
4
5
6
7
8
9
Method Two:
Class Res
{
private String name;
Private String sex;
Boolean flag=false;
Public synchronized void set (String name,string sex)
{
if (flag)
{
Tr
This article from "It Daniel share" blog, declined reprint!
Black Horse programmer _java08_ Multithreading