Java 线程
package 线程;
//先继承Thread
public class SimpleThread extends Thread{
public SimpleThread(String name ){//参数为线程的名称
setName(name);
}
public void run(){//覆盖run()方法
int i = 0 ;
while(i++ < 5 )//循环5 词
try {
System.out.println(getName()+"执行步骤"+i);
Thread.sleep(1000);//休眠1秒
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SimpleThread thread1 = new SimpleThread("线程1");
SimpleThread thread2 = new SimpleThread("线程2");
thread1.start();//启动线程1
thread2.start();//启动线程2
}
}
。>>>>>>>>>>>>>>>>>>>
package 线程;
//继承实现Runnable接口的方法
public class SimpleRunnable implements Runnable {
public void run(){//覆盖run方法
int i = 15 ;
while(i-- > 1){//循环5 次
try {
System.out.println("#");
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread thread1 = new Thread(new SimpleRunnable(),"线程1");
thread1.start();//启动线程1
}
}
>>>>>>>>>>
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
》》》》》》》》》》》》》》》》》》》》
线程的控制
》》》》》》》》》》》》》》》
》》》》》
》》》》》》》》》》》