zoukankan      html  css  js  c++  java
  • java 2中创建线程方法

      The Runnable 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 run.

    This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.

    In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run()method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

    Runnable 接口应该由那些打算通过某一线程执行其实例的类来实现。类必须定义一个称为 run 的无参数方法。

    设计该接口的目的是为希望在活动时执行代码的对象提供一个公共协议。例如,Thread 类实现了 Runnable。激活的意思是说某个线程已启动并且尚未停止。

    此外,Runnable 为非 Thread 子类的类提供了一种激活方式。通过实例化某个 Thread 实例并将自身作为运行目标,就可以运行实现 Runnable 的类而无需创建 Thread 的子类。大多数情况下,如果只想重写 run() 方法,而不重写其他 Thread 方法,那么应使用 Runnable 接口。这很重要,因为除非程序员打算修改或增强类的基本行为,否则不应为该类创建子类。

     

    void run()
    使用实现接口 Runnable 的对象创建一个线程时,启动该线程将导致在独立执行的线程中调用对象的 run 方法

    方法 run 的常规协定是,它可能执行任何所需的动作。

    Class Thread

    线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。

    每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新 Thread 对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。

    当 Java 虚拟机启动时,通常都会有单个非守护线程(它通常会调用某个指定类的 main 方法)。Java 虚拟机会继续执行线程,直到下列任一情况出现时为止:

    • 调用了 Runtime 类的 exit 方法,并且安全管理器允许退出操作发生。
    • 非守护线程的所有线程都已停止运行,无论是通过从对 run 方法的调用中返回,还是通过抛出一个传播到 run 方法之外的异常。

     创建新执行线程有两种方法。一种方法是将类声明为 Thread 的子类。该子类应重写 Thread 类的 run 方法。接下来可以分配并启动该子类的实例。例如,计算大于某一规定值的质数的线程可以写成:

     


         class PrimeThread extends Thread {
             long minPrime;
             PrimeThread(long minPrime) {
                 this.minPrime = minPrime;
             }
     
             public void run() {
                 // compute primes larger than minPrime
                  . . .
             }
         }
     

    然后,下列代码会创建并启动一个线程:

     

         PrimeThread p = new PrimeThread(143);
         p.start();
     

    创建线程的另一种方法是声明实现 Runnable 接口的类。该类然后实现 run 方法。然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。采用这种风格的同一个例子如下所示:

     


         class PrimeRun implements Runnable {
             long minPrime;
             PrimeRun(long minPrime) {
                 this.minPrime = minPrime;
             }
     
             public void run() {
                 // compute primes larger than minPrime
                  . . .
             }
         }
     

    然后,下列代码会创建并启动一个线程:

     

         PrimeRun p = new PrimeRun(143);
         new Thread(p).start();
     

    每个线程都有一个标识名,多个线程可以同名。如果线程创建时没有指定标识名,就会为其生成一个新名称

     

    Thread类有几个常用方法:

     

    public void start()
    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

    The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

    当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)

    It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

    多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。

     

    public static void sleep(long millis)
                      throws InterruptedException
    在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)
     public static void sleep(long millis, int nanos) 
              在指定的毫秒数加指定的纳秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响
     
    使用Thread.sleep()可以是我们的方法延迟调用:
    try{
    Thread.sleep(30000); //单位为毫秒
    }catch(Exception e){}
    System.out.print("hello");
    30秒之后才会输出hello
     
  • 相关阅读:
    URL域名获取
    SQL Server 索引结构及其使用(二)
    SQL Server 索引结构及其使用(一)[转]
    查询数据库中所有表的数据量、有效数据量以及其它定制数据量
    转:Servlet的url匹配以及url-pattern详解
    转:在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程
    MyEclipse +Servlet 乱码
    MyEclipse +Tomcat 异常操作
    Android Include标签
    转ATL对象类型
  • 原文地址:https://www.cnblogs.com/youxin/p/2459537.html
Copyright © 2011-2022 走看看