第十七节:Runnable创建线程,Thread创建线程,唤醒线
2018-08-01 本文已影响0人
魔王哪吒

Runnable创建线程
public class RunnableDemo implements Runnable{
@Override
public void run(){
int i = 1;
while(true) {
System.out.println(Thread.currentThread().getName()+"线程名字"+i);
i++;
if(i>30){
break;
}
try{
Thread.currentThread().sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
Thread zhangshan = new Thread(new RunnableDemo());
zhangshan.setName("zhangshan");
zhangshan.start();
}
Thread创建线程
public class ThreadDemo extends Thread{
@Override
public void run(){
super.run();
int i=1;
while(true){
System.out.println(this.getName()+"线程名"+i);
i++;
if(i>30){
break;
}
try{
this.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
ThreadDemo zhangshan = new ThreadDemo();
zhangshan.setName("zhangshan");
zhangshan.start();
}
}
唤醒线程
线程类

主方法类

计数器多线程
实现接口

run方法

成员变量

完成按钮中的方法

线程同步与等待
