使用Runnable接口创建线程-3

实现Runnable接口的类必须使用Thread类的实例才能创建线程。通过Runnable接口创建线程分为两步:

1. 将实现Runnable接口的类实例化。

2. 建立一个Thread对象,并将第一步实例化后的对象作为参数传入Thread类的构造方法。

最后通过Thread类的start方法建立线程。

 package com.fly.example;

 public class MyRunnableThree implements Runnable {

     @Override
public void run()
{
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args)
{
MyRunnableThree t1 = new MyRunnableThree();
MyRunnableThree t2 = new MyRunnableThree();
MyRunnableThree t3 = new MyRunnableThree();
Thread thread1 = new Thread(t1, "MyThread1");
Thread thread2 = new Thread(t2);
Thread thread3 = new Thread(t3);
thread2.setName("MyThread2");
thread1.start();
thread2.start();
thread3.start(); } }
运行结果如下:
MyThread1
MyThread2
Thread-1
上一篇:sqlserver2008r2安装


下一篇:实现Callable接口创建线程