线程的操作 继承 thread 和实现renable 接口
//线程的各个方法
t.isAlive();
//是否存活
t.resume();
//强制关闭
package xiancheng;
//继承thread 实现线程
public class Student extends Thread{
public void run(){
this.getName();
for (int i = 0; i <50; i++) {
try {
//线程迟钝300毫秒
Thread.currentThread().sleep(300);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+(i+1));
}
}
}
//实现runnable接口来实现线程
package xiancheng;
public class Teacher implements Runnable {
public void run(){
for (int i = 0; i <50; i++) {
try {
Thread.currentThread().sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+(i+1));
}
}
}
//测试类
package xiancheng;
public class Main {
public static void main(String[] args) {
Student st=new Student();
st.setName("明明");
st.start();
Thread.currentThread().setName("main");
Runnable rn=new Teacher();
Thread tr=new Thread(rn);
tr.setName("介个老师");
tr.start();
for (int i = 0; i < 50; i++) {
try {
Thread.currentThread().sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+(i+1));
}
}
}