多线程BackGround小案例

来源:互联网 发布:淘宝实名认证账号出售 编辑:程序博客网 时间:2024/06/05 09:56

BackGroundTread 实现runnable接口

package cn.happy.tread3;


public class BackGroundTread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
System.out.println("这是后台线程第"+i+"次执行");
}

}
}


MyTread继承Thread类

package cn.happy.tread3;


public class MyTread extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
System.out.println("这是线程第"+i+"次执行");
}
}
}


测试类

package cn.happy.tread3;


public class Test {public static void main(String[] args) {
Thread t1=new MyTread();
Thread t2=new Thread(new BackGroundTread());
t2.setDaemon(true);
t1.start();
t2.start();
}


}

原创粉丝点击