首页 > 编程语言 >Java并发之回顾Thread和runnable

Java并发之回顾Thread和runnable

时间:2022-11-16 21:36:49浏览次数:44  
标签:runnable run Thread thread method Runnable Java class

jdk 文档的描述

Thread

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:

  class PrimeThread extends Thread {
      long minPrime;
      PrimeThread(long minPrime) {
          this.minPrime = minPrime;
      }

      public void run() {
          // compute primes larger than minPrime
           . . .
      }
  }

The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143);
p.start();

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following:

  class PrimeRun implements Runnable {
      long minPrime;
      PrimeRun(long minPrime) {
          this.minPrime = minPrime;
      }

      public void run() {
          // compute primes larger than minPrime
           . . .
      }
  }

The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun(143);
new Thread(p).start();

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Runnable

/**
 * The <code>Runnable</code> interface should be implemented by any
 * class whose instances are intended to be executed by a thread. The
 * class must define a method of no arguments called <code>run</code>.
 * <p>
 * This interface is designed to provide a common protocol for objects that
 * wish to execute code while they are active. For example,
 * <code>Runnable</code> is implemented by class <code>Thread</code>.
 * Being active simply means that a thread has been started and has not
 * yet been stopped.
 * <p>
 * In addition, <code>Runnable</code> provides the means for a class to be
 * active while not subclassing <code>Thread</code>. A class that implements
 * <code>Runnable</code> can run without subclassing <code>Thread</code>
 * by instantiating a <code>Thread</code> instance and passing itself in
 * as the target.  In most cases, the <code>Runnable</code> interface should
 * be used if you are only planning to override the <code>run()</code>
 * method and no other <code>Thread</code> methods.
 * This is important because classes should not be subclassed
 * unless the programmer intends on modifying or enhancing the fundamental
 * behavior of the class.
 *
 * @author  Arthur van Hoff
 * @see     java.lang.Thread
 * @see     java.util.concurrent.Callable
 * @since   JDK1.0
 */
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Thread 类的构造函数剖析

标签:runnable,run,Thread,thread,method,Runnable,Java,class
From: https://www.cnblogs.com/tangyouwei/p/review-thread-and-runnable.html

相关文章

  • JavaScript中点击按钮弹出新的浏览器窗口
    */*Copyright(c)2016,烟台大学计算机与控制工程学院*Allrightsreserved.*文件名:text.js*作者:常轩*微信公众号:Worldhello*完成日期:2016年10月226日*版本号:V......
  • Java练习1
    */*Copyright(c)2016,烟台大学计算机与控制工程学院*Allrightsreserved.*文件名:text.cpp*作者:常轩*微信公众号:Worldhello*完成日期:2016年9月20日*版本号:V1......
  • Javascript(笔记39) - ES6特性 - 集合Set
    SETES6 提供了新的数据结构set(集合)。集合类似于数组,但成员的值都是唯一的,集合实现iterator 接口,所以可以使用“扩展运算符”和“for...of”进行遍历,集合的属性和方法......
  • 肖sir__Java中spring boot
    1.1springboot介绍什么是SpringBoot?SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来......
  • [JavaScript]自定义排序方式Array.sort
    自定义排序方式,通过array.sort//按助力值、绑定时间排序。return<0:a在前,return>0:a在后,return==0:不变list.sort(function(a,b){varref=0if(a.bi......
  • 封装,继承(super,this,方法重写),多态--JAVA
    一、封装封装:就是把抽象出的数据【属性】和对数据的操作【方法】封装在一起,数据被保护在内部,程序的其他部分只有通过被授权的操作才能对数据进行操作  publicclass......
  • 一道容易犯错的Java String面试题
    分析下面这段代码,说明总共创建了多少个对象?程序的输出结果是什么?publicclassDemo{publicstaticvoidmain(String[]args){Stringa="hello";......
  • Java中ThreadLocal详解
    一、ThreadLocal简介        ThreadLocal叫做线程变量,意思是ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的......
  • Java 内存模型(JMM)
    1.为什么要有内存模型?要想回答这个问题,我们需要先弄懂传统计算机硬件内存架构。好了,我要开始画图了。1.1.硬件内存架构(1)CPU去过机房的同学都知道,一般在大型服务器上......
  • 【Java】Synchronized与ReentrantLock区别总结
    这篇文章是关于这两个同步锁的简单总结比较,关于底层源码实现原理没有过多涉及,后面会有关于这两个同步锁的底层原理篇幅去介绍。相似点:这两种同步方式有很多相似之处,它们......