} catch (InterruptedException e) {} }
} } 途径二:
class SimpleThread implements Runnable {
public SimpleThread(String str) { super(str);
}
public void run() { for (int i = 0; i < 10; i++) {
System.out.println(getName()+\被调用!\ try {
Thread.sleep(10);
} catch (InterruptedException e) {} } } }
3.什么是线程的生命期?它包括哪几种状态?它们的关系是什么?
答:线程的生命期是指从线程被创建开始到死亡的过程,通常包括5种状态:新建、就绪、运行、阻塞、死亡。它们的关系如图所示:
New Thread()新建状态start()就绪状态调度stop()时间片结束或调用yield()阻塞消除阻塞状态运行状态run()结束stop()wait()、sleep()、suspend()等psto死亡状态()
4.请举例说明如何实现线程的同步(用两种方法)。 方法一(方法同步): class Stack{
private int number; private int len=0; public synchronized void put(int t){ while(len == 1){ try{ this.wait(); }catch(InterruptedException e){} } number=t; len=1; this.notify(); } public synchronized int get(){ while(len ==0){ try{ this.wait(); }catch(InterruptedException e){} } len=0; this.notify(); return number; } }
方法二(对象同步): class Stack{ private int number; private int len=0; public void put(int t){ synchronized (this){ while(len == 1){ try{ this.wait(); }catch(InterruptedException e){} } number=t; len=1; this.notify(); } } public int get(){ synchronized (this){ while(len ==0){ try{ this.wait();
}catch(InterruptedException e){} } len=0; this.notify(); return number; } } }
class Input implements Runnable{ Stack sstack; public Input(Stack s){ sstack=s; } public void run(){ for(int i=1;i<5;i++){ sstack.put(i); System.out.println(\向Stack放入数字:\ try{ Thread.sleep((int)(Math.random()*10)); }catch(InterruptedException e){} } } }
class Output implements Runnable{ Stack sstack; public Output(Stack s){ sstack=s; }
public void run(){ int temp; for(int i=1;i<5;i++){ temp=sstack.get(); System.out.println(\向Stack取出数字:\ try{ Thread.sleep((int)(Math.random()*10)); }catch(InterruptedException e){} } } }
public class Hello { public static void main(String args[]){ Stack s = new Stack(); Input t=new Input(s); Output o = new Output(s);
}
Thread t1 = new Thread (t); Thread t2 = new Thread (o); t1.start(); t2.start(); }
5.Java中有哪些情况会导致线程的不可运行?
答:一是调用了wait()方法,使得线程等待;二是调用了sleep(int time)方法,使得线程休眠;三是调用了suspend()方法,使得线程挂起;四是由于输入输出流而引起阻塞。
6.wait()方法和sleep()方法的区别是什么?
答:sleep()方法使线程进入睡眠状态,但它并不会释放线程持有的资源,不能被其他资源唤醒,不过睡眠一段时间会自动醒过来,而wait()方法让线程进入等待状态的同时也释放了持有的资源,能被其他资源唤醒。
7.线程组的作用是什么?如何创建一个线程组?
答:线程组是把多个线程集成到一个对象里并可以同时管理这些线程。
在线程创建时,可以将线程放在某个指定的线程组中,也可以将它放在一个默认的线程组。若创建线程而不明确指定属于哪个组,它们就会自动归属于系统默认的线程组。以下三种Thread类的构造方法实现线程创建的同时指定其属于哪个线程组。
public Thread (ThreadGroup group,Runnable target); public Thread (ThreadGroup group,String name);
public Thread (ThreadGroup group,Runnable target,String name);
8.Java线程调度的原则是什么?
答:Java调度器调度遵循以下原则:优先级高的线程比优先级低的线程线程先调度,优先级相等的线程按
照排队队列的顺序进行调度,先到队列的线程先被调度。当一个优先级低的线程运行过程中,来了一个
高优先级线程,在时间片方式下,优先级高的线程要等优先级低的线程时间片运行完毕才能被调度,而在抢占式调度方式下,优先级高的线程可以立刻获得CPU的控制权。
9.如何理解死锁?
答:如果两个或多个线程都在互相等待对方持有的锁(唤醒),那么这些线程都进入阻塞状态,永远地等待下去,无法执行,程序就出现了死锁。
10.下列程序输出的结果是什么? class Daemon extends Thread {
public void run() {
if(this.isDaemon()==false)
}
}
System.out.println(\System.out.println(\
for(int i=0;i<10;i++){ }
System.out.println(i); Thread.sleep(200);
else try {
}catch (InterruptedException e){}
System.out.println(\
public class Test {
public static void main (String[] args) { }
Thread t=new Daemon(); t.setDaemon(true); t.start(); try {
Thread.sleep(900);
}catch (InterruptedException e){} System.out.println(\
}
答:输出结果为:thread is daemon
0 1 2 3 4
main done!
11.编写程序实现如下功能:一个线程进行如下运算1*2+2*3+3*4+……+1999*2000,而另一个线程则每隔一段时间读取前一个线程的运算结果。 解:
class Result{ private long total=1*2; public void write(long t){ total=total+t; } public long read(){