多线程

    xiaoxiao2021-03-25  93

    package cn.itcast_05; /* * 方式2:实现Runnable接口 * 步骤: * A:自定义MyRunnable实现Runnable接口 * B:重写run()方法 * C:创建MyRunnable类的对象 * D:创建Thread类对象,并把C步骤的对象作为构造参数的传递。 */ public class MyRunnableDemo { public static void main(String[] args) { // 创建MyRunnable类的对象 MyRunnable my = new MyRunnable(); // 创建Thread类对象,并把C步骤的对象作为构造参数的传递。 // public Thread(Runnable target) // Thread t1 = new Thread(my); // Thread t2 = new Thread(my); // t1.setName("风清杨"); // t2.setName("李晨"); // public Thread(Runnable target,String name) Thread t1 = new Thread(my, "风清杨"); Thread t2 = new Thread(my, "李晨"); // 启动线程 t1.start(); t2.start(); } }

    package cn.itcast_05; public class MyRunnable implements Runnable { @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(Thread.currentThread().getName() + ":" + x); } } }

    转载请注明原文地址: https://ju.6miu.com/read-25471.html

    最新回复(0)