62,623
社区成员
发帖
与我相关
我的任务
分享
package com.test;
/**
*
* @author botao ThreadDemo3 只是一个测试类而已. 其中通过线程的不同实现方式,进行不同的初始化.
*/
public class ThreadDemo3 {
public static void main(String args[]) {
// new TestThread ().start();
Thread t1 = new Thread(new TestThread());// 创建线程1,这中方式创建好象是通过代理的设计模式来实现多线程的.
t1.start();// 开启线程
Thread t2 = new TestThread2();// 创建线程2
t2.start();// 开启线程
// 主现成的任务
for (int i = 0; i < 100; i++) {
System.out.println("主要的" + "is running");
}
}
}
class TestThread implements Runnable {// 实现多现成的第一种方式,实现Runnable接口,实现run方法
/**
* run中写要做的事.
*/
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("次要一一一一一一一一一一一一一" + "is running");
}
}
}
class TestThread2 extends Thread {// 实现多现成的第二种方式,继承Thread,重写run方法.
/**
* run中写要做的事.
*/
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("次要二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二"
+ "is running");
}
}
}