刚刚自学线程部分,有点模模糊糊的,编写了一些代码,没有如预期执行,请各位高手指点。
下面是代码:import java.lang.*;
class Test extends Thread{
int flag;
public Test(int x){
this.flag=x;
}
public static void main(String argv[]){
Test t = new Test(1);
Test s = new Test(2);
t.setPriority(1);
s.setPriority(MAX_PRIORITY);
t.start();
System.out.println("one");
s.start();
System.out.println("two");
}
public void run(){
for(int i=0;i<3;i++){
if(flag==1){
System.out.println("Thread t");
}
else{
System.out.println("Thread s");
}
try{
Thread.sleep(3000);
Thread.interrupted();
}catch(InterruptedException e){
if(flag==1)System.out.println("t sleep");
else if(flag==2)System.out.println("s sleep"); }
}
}运行时,线程t和s确实都sleep了,但catch程序块中的代码始终未执行,不知道是什么原因呢?
另,通过t.setPriority(1);s.setPriority(MAX_PRIORITY);设置优先级后,由于s的优先级高于t,一定是s进程先被执行么~
谢谢各位!
下面是代码:import java.lang.*;
class Test extends Thread{
int flag;
public Test(int x){
this.flag=x;
}
public static void main(String argv[]){
Test t = new Test(1);
Test s = new Test(2);
t.setPriority(1);
s.setPriority(MAX_PRIORITY);
t.start();
System.out.println("one");
s.start();
System.out.println("two");
}
public void run(){
for(int i=0;i<3;i++){
if(flag==1){
System.out.println("Thread t");
}
else{
System.out.println("Thread s");
}
try{
Thread.sleep(3000);
Thread.interrupted();
}catch(InterruptedException e){
if(flag==1)System.out.println("t sleep");
else if(flag==2)System.out.println("s sleep"); }
}
}运行时,线程t和s确实都sleep了,但catch程序块中的代码始终未执行,不知道是什么原因呢?
另,通过t.setPriority(1);s.setPriority(MAX_PRIORITY);设置优先级后,由于s的优先级高于t,一定是s进程先被执行么~
谢谢各位!
解决方案 »
- poi 2.5 跨列居中
- 有关java.lang.ClassNotFoundException和Class not found的疑问
- 使用了2个开源类库,这两个类库都需要dom4j做支持,但需求的版本不一样,怎么处理?
- switch怎么返回继续判断?
- windows下面用JAVA进行USB通信?
- 装完ORACLE 9I,就打不开ECLIPSE了~说是找不到JRE了~这是怎么回事啊~
- [求助]applet在Jcreator里编译成功后,运行时却出错为什么?
- 请教高手!!!!!!!(up有分)
- 关于用applet实现打印的问题~急~~~~~!
- 求助:怎样设计返回文件夹对话框?
- 继承中的构造方法的问题
- SCJP 6.0認證教戰手冊考題.232--线程
Test t = new Test(1);
Test s = new Test(2);
t.setPriority(1);
s.setPriority(MAX_PRIORITY);
t.start();
System.out.println("one");
s.start();
//public void interrupt() 中断线程。
t.interrupt();
System.out.println("two");
}
应该是先执行高优先级的在执行低优先级的
class TestThreadPriority extends Thread {
int flag;
TestThreadPriority ttp; public TestThreadPriority(int x) {
this.flag = x;
} public TestThreadPriority(int x, TestThreadPriority ttp) {
this.flag = x;
this.ttp = ttp;
} public static void main(String argv[]) {
TestThreadPriority s = new TestThreadPriority(2);
TestThreadPriority t = new TestThreadPriority(1, s);
t.setPriority(1);
s.setPriority(MAX_PRIORITY);
t.start();
System.out.println("one");
s.start();
System.out.println("two");
} public void run() {
for (int i = 0; i < 3; i++) {
if (flag == 1) {
System.out.println("Thread t");
try {
ttp.interrupt();
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("t sleep");
}
} else {
System.out.println("Thread s");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("s sleep");
}
}
}
}
}
是不是一个线程sleep时,可能会被别的线程打断也可能不会,线程不被打断的时候就不会有InterruptedException呢?是不是某个线程比正在休眠的线程优先级高,就会打断休眠的线程呢?
之前看到一本书上写的sleep函数必将导致InterruptedException,所以必须catch,可能是对这句话理解错误了。
而被锁互斥的阻塞和I/o流的阻塞(除了管道流以外)都是不可被Interrupted的
lz对异常的概念不是很清楚 再看看相关资料吧