zoukankan      html  css  js  c++  java
  • 开启线程的方式。

    方法一:直接继承Thread类

    public class Demo_Thread {
    public static void main(String[] args) {
    Mythread mt = new Mythread(); // 4.创建Thread类的子类对象
    mt.start(); // 5.开启线程
    for (int i = 0; i < 1000; i++) {
    System.out.println("dddd");
    }
    }
    }

    class Mythread extends Thread { // 1.继承Thread
    public void run() {// 2.重写run方法
    for (int i = 0; i < 1000; i++) { // 3.将要执行的代码写在run方法中
    System.out.println("555");
    }
    }
    }

    方法二:创建线程的另一种方法是声明实现Runnable接口的类,该类然后实现run方法,然后可以分配该类的实例,在创建Thread时作为一个参数来传递并启动。

    public class Demo2_Thread {
    public static void main(String[] args) {
    MyRunnable mr = new MyRunnable(); //4.创建Runnable的子类对象
    Thread t = new Thread(mr); //5.将其当做参数传递给Thread的构造函数
    t.start(); //6.开启线程
    for (int i = 0; i < 1000; i++) {
    System.out.println("66666666");
    }
    }
    }

    class MyRunnable implements Runnable { //1.定义一个类实现Runnable接口

    @Override
    public void run() { //2.重写run方法
    // TODO Auto-generated method stub

    for (int i = 0; i < 1000; i++) { // 3.将要执行的代码写在run方法中
    System.out.println("555");
    }
    }

    }

  • 相关阅读:
    Javascript
    CSS3新增特性HTML标签类型
    HTML5新增的标签
    prototype __proto__ Function
    oninput
    extend
    hasOwnProperty()
    this prototype constructor
    Array类型判断
    指针
  • 原文地址:https://www.cnblogs.com/wangffeng293/p/13308483.html
Copyright © 2011-2022 走看看