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");
    }
    }

    }

  • 相关阅读:
    Jersey Politics
    网络流——最小费用最大流
    网络流——最大流Dinic算法
    【洛谷2756】飞行员配对方案问题(二分图匹配,网络流24题)
    状压dp入门
    2018九江市赛
    [CQOI2007]余数求和
    CSAPC2008 skyline
    [ZJOI2009]函数 题解
    由不定方程想到的——数论选讲
  • 原文地址:https://www.cnblogs.com/wangffeng293/p/13308483.html
Copyright © 2011-2022 走看看