public class ExampleThread extends Thread{
@Override
public void run() {
super.run();
System.out.println("这是一个继承自Thread的ExampleThread");
}
}
public class ExampleRunable implements Runnable{
public void run() {
System.out.println("这是实现Runnable接口的类");
}
}
public class ExampleCurrentThread extends Thread{
public ExampleCurrentThread(){
System.out.println("构造方法的打印:" + Thread.currentThread().getName());
}
@Override
public void run() {
super.run();
System.out.println("run方法的打印:" + Thread.currentThread().getName());
}
}
public class ExampleCurrentThreadTest extends TestCase {
public void testInit() throws Exception{
ExampleCurrentThread thread = new ExampleCurrentThread();
}
public void testRun() throws Exception {
ExampleCurrentThread thread = new ExampleCurrentThread();
thread.start();
Thread.sleep(1000);
}
}
构造方法的打印:main run方法的打印:Thread-0 构造方法的打印:main
public class ComplexCurrentThread extends Thread{
public ComplexCurrentThread() {
System.out.println("begin=========");
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
System.out.println("this.getName()=" + this.getName());
System.out.println("end===========");
}
@Override
public void run() {
super.run();
System.out.println("run begin=======");
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
System.out.println("this.getName()=" + this.getName());
System.out.println("run end==========");
}
}
public class ComplexCurrentThreadTest extends TestCase {
public void testRun() throws Exception {
ComplexCurrentThread thread = new ComplexCurrentThread();
thread.setName("byhieg");
thread.start();
Thread.sleep(3000);
}
}
begin========= Thread.currentThread().getName=main this.getName()=Thread-0 end=========== run begin======= Thread.currentThread().getName=byhieg this.getName()=byhieg run end==========
public class AliveThread extends Thread{
@Override
public void run() {
super.run();
System.out.println("run方法中是否存活" + " " + Thread.currentThread().isAlive());
}
}
public class AliveThreadTest extends TestCase {
public void testRun() throws Exception {
AliveThread thread = new AliveThread();
System.out.println("begin == " + thread.isAlive());
thread.start();
Thread.sleep(1000);
System.out.println("end ==" + thread.isAlive());
Thread.sleep(3000);
}
}
begin == false run方法中是否存活 true end ==false
public class ExampleInterruptThread extends Thread{
@Override
public void run() {
super.run();
try{
for(int i = 0 ; i < 50000000 ; i++){
if (interrupted()){
System.out.println("已经是停止状态,我要退出了");
throw new InterruptedException("停止.......");
}
System.out.println("i=" + (i + 1));
}
}catch (InterruptedException e){
System.out.println("顺利停止");
}
}
}
public class ExampleInterruptThreadTest extends TestCase {
public void testRun() throws Exception {
ExampleInterruptThread thread = new ExampleInterruptThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
/** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有