创建多线程方式:
1.通过创建Thread子类对象,并重写run方法,创建一个线程:
Thread thread = new Thread(){
@Override
public void run() {
while(true)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1"+Thread.currentThread().getName());
}
}
};
thread.start();
2.通过创建Runnable实现类,创建一个线程
Thread thread2 =new Thread( new Runnable(){
@Override
public void run() {
while(true)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("2"+Thread.currentThread().getName());
}
}
});
thread2.start();