Home > Java > javaTutorial > body text

Java thread (1) - Detailed explanation of thread status and attributes

黄舟
Release: 2017-03-01 11:38:32
Original
1593 people have browsed it


In the operating system, we use the time-sharing method to continuously switch and process multiple process tasks on the CPU, giving people the feeling of parallel processing. This method is called in the operating system Multitasking. Multitasking extends the concept of multithreading at a lower level, which means that a program executes multiple threads at the same time. This kind of program that can run more than one thread at the same time is called a multi-threaded program.

(1) Create a thread

1. Create a thread instance

It is better to see it than to talk about threads first Various states, why don't we directly demonstrate how to create a new thread.

package Thread;/**
 * 
 * @author QuinnNorris
 * 创建线程实例
 */public class NewThread {

    /**
     * @param args
     */
    public static void main(String[] args) {        // TODO Auto-generated method stub

        Runnable r = new Run();        //创建一个类对象
        Thread th = new Thread(r);        //由Runnable创建一个Thread对象
        th.start();        //调用start方法,运行新的线程,即运行的是th中的run方法
    }

}
Copy after login

Let’s take a look at Run, a class that implements the Runnable interface:

package Thread;/**
 * 
 * @author QuinnNorris
 * Run类实现了Runnable接口
 */public class Run implements Runnable {

    @Override
    public void run() {        // TODO Auto-generated method stub
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
Copy after login

Creating and running a thread is probably like this. We first create a class to implement the Runnable interface. Among them, the only run method in the Runnable interface is implemented. This method is automatically called after the thread is started. Then we create an object of the Run class in the main class and pass this object into the Thread object as a parameter of the constructor. Then we call the start method with the parameters of the Thread object. The start method will automatically call the run method and start executing the run. statement in the method. At this time, a thread is created and started.

2.start method and run method

It should be noted that we cannot call the run method directly because this will not create a new thread, but call the In the middle of an existing thread, start will create a new thread for you and automatically call run. We do not need to manually call the run method ourselves under any circumstances.
Speaking of which, let’s take a look at the start method in the API:

public void start( )
Cause the thread to start executing;
The Java virtual machine calls the run method of the thread.
The result is that two threads run concurrently; the current thread (returning from the call to the start method) and the other thread (executing its run method).
It is illegal to start a thread multiple times. Especially when a thread has finished executing, it cannot be restarted.

3.Runnable class

We already know how to create a thread step by step, but this does not satisfy our curiosity. We need to directly Take a closer look at these two classes to see what methods and properties they contain. There are no attributes in the Runnable class, only one run method. This interface should be implemented by classes that intend to execute their instances via a thread. The Thread class is more complicated.

(2) Thread class

1. The Thread class implements the Runnable interface

So in the above example, the Run class has A natural way of writing is to inherit the Thread class, because Thread implements Runnable. Both of these ways of writing actually override the run method. But interview questions like to ask you very much: Which of the two ways to implement multi-threading is better, Thread or Runnable? At this time, you should answer the Runnable method. The reason is also very simple. There can only be one inheritance, but there can be many interfaces. Runnable is an interface that does not hinder the flexibility of inheritance of this class.

2.Thread constructor

There are many constructors for Thread, and there are mainly four parameters:

public Thread(ThreadGroup group,Runnable target, String name,long stackSize)
Copy after login

The other constructors are The combination of these four parameters, then let’s take a look at the meaning of these four parameters:

1.ThreadGroup group

ThreadGroup means thread group, it can Classify and manage a batch of threads. The control and management of the thread group means controlling the batch of threads in the thread group at the same time. All threads created by the user belong to the specified thread group. If no thread group is specified, the thread belongs to the default thread group (ie, main thread group). By default, the child thread and the parent thread are in the same thread group. The thread group to which a thread belongs can only be specified when it is created. The thread group to which it belongs cannot be changed while a thread is running. That is to say, once a thread specifies the thread group to which it belongs, it will not be changed until the thread ends. The structure between thread groups and threads is similar to a tree structure. A thread can only access the information of its own thread group, but cannot access the information in the parent thread group or other thread groups.

2.Runnable target

target is the object whose run method is called. If this parameter is null, the run method must be rewritten in the Thread class. Otherwise, the run method in the Thread class calls the run method in the Runnable class.

3.String name

Each thread has its own name. In fact, this parameter is rarely used. Sometimes we name a thread and Don't use this form. But if we don't fill in this parameter, the thread will automatically generate a new name. The automatically generated name is of the form "Thread-"+n, where n is an integer.

4.long stackSize

这是一种具有平台依赖性的参数,stackSize能指定堆栈的大小。 在某些平台上,指定一个较高的 stackSize 参数值可能使线程在抛出 StackOverflowError 之前达到较大的递归深度。stackSize 参数的值与最大递归深度和并发程度之间的关系细节与平台有关。在某些平台上,stackSize 参数的值无论如何不会起任何作用。 作为建议,可以让虚拟机自由处理 stackSize 参数。

Thread中还有很多很多实用的方法,我们在涉及到具体概念的时候再给予介绍。

(三)中断线程

我们既然创建了线程,那么这个线程在什么时候终止呢?有两种情况:

  1. 当线程的run方法执行方法体中最后一条语句后,正常退出而自然死亡。

  2. 出现了在方法中没有捕获的异常,此时终止run方法,意外死亡。

从这两点中,我们可以看出,其实我们并没有特殊的手段可以人为的去在中间干涉线程的中断(Thread中的stop方法或许有这种作用,但是这个方法已经被废弃,我们不要使用它)。虽然没有强制结束线程的方法,但是我们可以用interrupt方法请求终止线程。要注意“请求”这个词,没有任何语言方面的表述是要求一个被中断的线程应该被终止。我们去用interrupt方法中断一个线程不过是引起他的注意。被中断的线程可以决定如何去响应这个中断的请求。

当对一个线程调用interrupt方法时,线程的中断状态将被置位。这个中断状态是每个线程都有的boolean标志。每个线程都会不时地检查这个布尔值,判断这个线程是否被中断。
我们可以使用:

Thread.currentThread().isIntertrupted()
Copy after login

isInterrupt方法和interrupt很像,它是用来判断线程是否被中断。
我们可以通过interrupt方法来中断一个线程,但是值得注意的是,如果这个线程处于wait和sleep或者在join方法调用过程中,中断线程将会报一个InterruptedException异常,我们可以通过try-catch块来捕获这种异常。

(四)线程状态

线程一共有6种状态,这六种状态不是我们规定的,而是在Thread中的内部嵌套类State中规定的。这六种状态分别是New,Runnable,Blocked,Waiting,Timed waiting,Terminated六种,我们来逐个分析一下这几种状态的含义:

1.New 新创建的线程

这里的创建新的线程真的是仅仅new了一个线程。

new Thread(r);
Copy after login

创建新的线程,是指刚刚new出来的线程,这个线程没有通过start的方法来启动。

2.Runnable 可运行

那么一旦我们调用了start方法,这个线程开始工作。这是他就处于可运行状态,这个可运行状态不只是包含线程运行的时候,线程在中断的时候也被算为可运行状态。一个可运行状态的线程可能在运行也可能没在运行,我们不要因为一个线程在可运行的状态下没运行而急躁,很有可能这个线程的终止只是为了让其他的线程获得机会。

3.Blocked 被阻塞

当一个线程试图去获得一个内部锁时,但这个内部锁被其他的线程持有,这个时候,为了等待去使用这个内部锁,这个线程将会暂时处在被阻塞的状态。当其他线程释放锁的时候,这个线程获得了内部锁,并且从阻塞状态转变为非阻塞状态。

4.Wait 等待

当一个线程等待另一个线程通知调度器一个条件时,这个线程自己进入等待状态。等待状态和阻塞状态很类似,但是他们是存在本质区别的。如果另一个线程通知调度器结束,那么这个线程进行工作,等待状态也随之结束。

5.Timed waiting 计时等待

计时等待和等待是比较相似的,计时等待是表示他有一个超时参数。调用他们导致线程会进入计时等待。这个状态将一直保持到超市期满或者接收到适当的通知。相比较直接的等待,变得更加的安全。

6.Terminated 终止

线程的终止,我们在上面的线程中断中有所提及。线程终止理论上只有两种情况:当线程的run方法执行方法体中最后一条语句后,正常退出而自然死亡。2. 出现了在方法中没有捕获的异常,此时终止run方法,意外死亡。

(五)线程属性

线程中有很多的属性,尽管我在API中只看到一些其中一小部分的字段,但是线程的这种理念,在方法中也有所体现。线程的属性有以下这些:线程优先级,守护线程,处理未捕获异常的处理器。

1.线程优先级

在java中,每个线程都有一个优先级,我们需要知道的是,在没经过特殊处理的时候,所有的线程优先级都是一样的。默认的,我们把优先级分成1到10之间,高优先级的线程会先被操作。说到这里不由得让我们想起了操作系统中的进程优先级,和一些类似老化这样的名词。实际上,java虚拟机也确实是用进程的优先级来类比出线程的优先级,这样做最大的一个问题就在于,每个操作系统对于进程优先级的处理并不相同,java的线程优先级也因此而具有平台变化。实际上,我们不应该把程序的正确性依赖于线程优先级,我们应该尽量少的使用线程优先级。我们在这里介绍了线程优先级,但是我们并不建议去使用它。

2.守护线程

守护线程是指通过:

t.setDaemon(ture); //将线程转换为守护线程
Copy after login

这样的写法,将一个线程转换为守护线程。守护线程的作用是为其他的线程提供服务,如果其他所有的线程都被退出,只剩下守护线程,那么程序也就结束了。没有去单独运行守护线程的必要。比如说其他线程的计时器,我们就可以将它设置为一个守护线程。

3.处理未捕获异常的处理器

run方法不能抛出任何被检查,不被检测的异常会导致线程终止。在这种情况下,线程就死亡了。在这种情况下,我们需要实现一个Thread.UncaughtExceptionHandler。我们可以通过这种方式来知道,真正让我们的run意外死亡的问题在哪里。

(六)总结

在java的并发程序中,我们先了解了这些线程的状态和属性,下面我们就可以来研究关于线程之间同步的问题。如何能几个线程同时运行,让他们之间配合的好。

在操作系统中,我们通过分时的方法在CPU上不断地切换处理多个进程任务,给人并行处理的感觉,这种方法在操作系统中叫做多任务。多任务在较低层次上扩展出多线程的概念,也就是指一个程序同时执行多个线程。这种可以同时运行一个以上的线程的程序,我们叫做多线程程序。

 以上就是java线程(一)—线程状态及属性详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template