zoukankan      html  css  js  c++  java
  • 多线程第二种方式-实现runnable

    package runnable.demo;
    /*
     * 线程的第二种实现方式 - 实现 runnable 的接口
     * 备注: start()方式是 thread 独有的,如果用runnable 实现的话 就不能用 start()方式开启线程
     * 解决方法:thread 类提供了两种构造方法 ,接收runnable子类的实例化对象
     * 1、   分配新的 Thread 对象。
     * 2、Thread(Runnable target, String name)      分配新的 Thread 对象。
      
     */
    // 写一个类去实现 runnable接口
    class RunnableDemo implements Runnable {
        //构造方法为属性赋值
        private String name;
        public RunnableDemo(String name){
            this.name = name;
        }
        //重写run()方法
        public void run(){
            for (int i = 0; i <10; i++) {
                System.out.println(name+"i="+i);
            }
        }
    }
    public class ImplementRunnableDemo {
      public static void main(String[] args) {
        // new 两个实例化对象出来
          RunnableDemo  rd = new RunnableDemo("线程A");
          RunnableDemo  rd2 = new RunnableDemo("线程A");
          //通过 thread 构造方法 创造新的Thread 对象   Thread(Runnable target) 
          Thread th1 =new Thread(rd);
          Thread th2 = new Thread(rd2);
          //因为 th1,th2 是thread类型的,所以可以调用start()方法
          th1.start();
          th2.start();
         
          
    }
    }
  • 相关阅读:
    Lightoj 1422
    BZOJ 1801 [AHOI 2009] 中国象棋(DP)
    [SCOI2008]天平
    [SCOI2008]奖励关
    [USACO08JAN]haybale猜测Haybale Guessing
    [Sdoi2016]征途
    [SHOI2014]概率充电器
    [USACO08JAN]电话线Telephone Lines
    [HEOI2016]排序
    友好的生物
  • 原文地址:https://www.cnblogs.com/yuanyuan2017/p/6944742.html
Copyright © 2011-2022 走看看