package com.you;
import java.util.Date;
/**
* 多线程的实现方式
* 1、继承Thread类
* 2、实现Runnable接口
* @author chongrubujing
*
*/
public class ThreadDemo {
public static void main(String[] args) {
//使用自定义线程1
Mythread mythread = new Mythread();
mythread.start();//启动线程
//使用自定义线程2
Myrunnable myrunnable = new Myrunnable();
Thread thread1 = new Thread(myrunnable);
thread1.start();
//一般使用匿名的方式(一个匿名对象,一个接口式的匿名内部类)
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
}).start();
//第一步:new Thread().start();
/*第二步:接口式的匿名内部类
* new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
}).start();*/
}
}
//自定义线程类1
class Mythread extends Thread
{
public void run()
{
for (int i = 0; i < 10; i++) {
System.out.println(new Date()+”-“+i);
}
}
}
//自定义线程类2
class Myrunnable implements Runnable
{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Myrunnable-"+i);
}
}
}