wj-conquer


======================================================
注:本文源代码点此下载
======================================================

import java.io.*;

//多线程编程

public class multithread {

public static void main(string args[]) {

system.out.println("我是主线程!");

// 下面创建线程实例thread1

threaduseextends thread1 = new threaduseextends();

// 创建thread2时以实现了runnable接口的thhreaduserunnable类实例为参数

thread thread2 = new thread(new threaduserunnable(), "secondthread");

thread1.start();// 启动线程thread1使之处于就绪状态

// thread1.setpriority(6);//设置thread1的优先级为6

// 优先级将决定cpu空出时,处于就绪状态的线程谁先占领cpu开始运行

// 优先级范围1到10,min_priority,max_priority,norm_paiority

// 新线程继承创建她的父线程优先级,父线程通常有普通优先级即5norm_priority

system.out.println("主线程将挂起7秒!");

try {

thread.sleep(7000);// 主线程挂起7秒

} catch (interruptedexception e) {

return;

}

system.out.println("又回到了主线程!");

if (thread1.isalive()) {

thread1.stop();// 如果thread1还存在则杀掉他

system.out.println("thread1休眠过长,主线程杀掉了thread1!");

} else

system.out.println("主线程没发现thread1,thread1已醒顺序执行结束了!");

thread2.start();// 启动thread2

system.out.println("主线程又将挂起7秒!");

try {

thread.sleep(7000);// 主线程挂起7秒

} catch (interruptedexception e) {

return;

}

system.out.println("又回到了主线程!");

if (thread2.isalive()) {

thread2.stop();// 如果thread2还存在则杀掉他

system.out.println("thread2休眠过长,主线程杀掉了thread2!");

} else

system.out.println("主线程没发现thread2,thread2已醒顺序执行结束了!");

system.out.println("程序结束按任意键继续!");

try {

system.in.read();

} catch (ioexception e) {

system.out.println(e.tostring());

}

}// main

}// multithread

class threaduseextends extends thread

// 通过继承thread类,并实现它的抽象方法run()

// 适当时候创建这一thread子类的实例来实现多线程机制

// 一个线程启动后(也即进入就绪状态)一旦获得cpu将自动调用它的run()方法

{

threaduseextends() {

}// 构造函数

public void run() {

system.out.println("我是thread子类的线程实例!");

system.out.println("我将挂起10秒!");

system.out.println("回到主线程,请稍等,刚才主线程挂起可能还没醒过来!");

try {

sleep(10000);// 挂起5秒

} catch (interruptedexception e) {

return;

}

// 如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉

// 但如果休眠时间过长,则线程还存活,可能被stop()杀掉

}

}

class threaduserunnable implements runnable

// 通过实现runnable接口中的run()方法,再以这个实现了run()方法的类

// 为参数创建thread的线程实例

{

// thread thread2=new thread(this);

// 以这个实现了runnable接口中run()方法的类为参数创建thread类的线程实例

threaduserunnable() {

}// 构造函数

public void run() {

system.out.println("我是thread类的线程实例并以实现了runnable接口的类为参数!");

system.out.println("我将挂起1秒!");

system.out.println("回到主线程,请稍等 jn0-120 e20-040 ,刚才主线程挂起可能还没醒过来!");

try {

thread.sleep(1000);// 挂起5秒

} catch (interruptedexception e) {

return;

}

// 如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉

// 但如果休眠时间过长,则线程还存活,可能被stop()杀掉

}

}


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值