java多线程 实现Runnable 接口

编写代码

package com.xiang.lesson03;

//创建线程方式2, 实现Runnable 接口,重写run 方法,
public class TestThread1 implements  Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("循环---+"+i);
        }
    }
//这是一个交替执行的过程
    public static void main(String[] args) {
//        创建Runnable 接口 的实现类
        TestThread1 thread1 = new TestThread1();
        //        创建线程对象,通过线程对象来开启我们的线程,代理;
//        Thread thread = new Thread(thread1);
//        thread.start();

        new  Thread(thread1).start();
        for (int i = 0; i < 10; i++) {
            System.out.println("循环第"+i+"次");
        }
    }

}

运行结果

循环第0次
循环---+0
循环第1次
循环第2次
循环第3次
循环第4次
循环第5次
循环第6次
循环第7次
循环第8次
循环第9次
循环---+1
循环---+2
循环---+3
循环---+4
循环---+5
循环---+6
循环---+7
循环---+8
循环---+9

Process finished with exit code 0

原文地址:https://www.cnblogs.com/d534/p/15239687.html