看线程的时候这里老看不明,谁能具体说说吗?最好能举个例子。谢谢。
解决方案 »
- 问几个Object转换到String的问题
- 用C/S模式做的JAVA QQ,S端导出JAR包后连不上数据库,连不上1433端口,请高手们支下招吧。。。
- a=a+1;a+=1;a++
- 为什么SendMail系统编译成.exe文件后发送不成功!急急。。在线等。。。
- 问大家一个问题[参加有分]
- 跪求高手教我如何用jtable读数据库的多行信息
- 大伙帮忙看看着个读取文件名,并输出文件字节的IO程序?
- 我新学Java,我想用J2sdk来写Java程序,可是我安装后,好象要我选择服务器.....?请进。
- 数据类型转换问题
- interface 到底有什么用???实现接口,怎么实现??
- 包的路径的问题,新手盼高手解答一二
- 请问用jdbc 访问sqlserver 2000 时,是不是一定要安装sqlserver的补丁 sp3?
* Waits at most <code>millis</code> milliseconds for this thread to
* die. A timeout of <code>0</code> means to wait forever.
*
* @param millis the time to wait in milliseconds.
* @exception InterruptedException if another thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
*/
public void run(){
System.out.println("线程退出");
}
public static void main(String[] args){
Thread t=new Thread(new MyThread());
t.start();
t.join();
System.out.prinln("那个线程已经死了!");
}}
Waits for this thread to die. void join(long millis)
Waits at most millis milliseconds for this thread to die. void join(long millis, int nanos)
Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.
public class MyThread implements Runnable{
public void run(){
System.out.println("线程开始");
}
public static void main(String[] args){
Thread t=new Thread(new MyThread());
t.start();
try{
System.out.println(t.isAlive());
t.join();
System.out.println("线程已退出");
System.out.println(t.isAlive());
}catch(java.lang.InterruptedException ex){
System.out.println(ex);
System.out.println("那个线程已经死了!");
}
}}
线程B.join();
则线程A会在此处进入休眠状态,直到线程B执行完毕,线程A才会继续执行,至于是立即获得CPU还是进入等待状态竞争CPU,API文档中并没有说明,不得而知啊,哪位高手知道?
private int duration;
public Sleeper(String name, int sleepTime) {
super(name);
duration = sleepTime;
start();
}
public void run() {
try {
sleep(duration);
} catch (InterruptedException e) {
System.out.println(getName() + " was interrupted. " +
"isInterrupted(): " + isInterrupted());
return;
}
System.out.println(getName() + " has awakened");
}
}
class Joiner extends Thread {
private Sleeper sleeper;
public Joiner(String name, Sleeper sleeper) {
super(name);
this.sleeper = sleeper;
start();
}
public void run() {
try {
sleeper.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " join completed");
}
}
public class Joining {
private static Test monitor = new Test();
public static void main(String[] args) {
Sleeper
sleepy = new Sleeper("Sleepy", 1500),
grumpy = new Sleeper("Grumpy", 1500);
Joiner
dopey = new Joiner("Dopey", sleepy),
doc = new Joiner("Doc", grumpy);
grumpy.interrupt();
}
}Sleeper是一个会睡上一段时间的Thread,至于睡多长时间,这要由构造函数的参数决定。Sleeper的run( )的sleep( )可以因时限到期而返回,也可以被interrupt( )打断。catch语句在报告中断的同时,会一并报告isInterrupted( )。当有别的线程调用了本线程的interrupt( )时,会设置一个标记以表示这个这个线程被打断了。当本线程捕获这个异常的时候,会清除这个标志。所以catch语句会永远报告说isInterrupted( )是false。这个标记是用来应付其它情况的,或许在没出异常的情况下,线程要用它来检查自己是不是被中断了。Joiner是另一个线程,它调用了Sleeper的join( ),所以它要等Sleeper醒过来。main( )创建了两个Sleeper分派给两个Joiner。你会发现,不论Sleeper是被打断还是正常结束,Joiner都会随Sleeper一道结束。
线程B.join();
则线程A会在此处进入休眠状态,直到线程B执行完毕,线程A才会继续执行,至于是立即获得CPU还是进入等待状态竞争CPU,API文档中并没有说明,不得而知啊,哪位高手知道?--------------------------------------------------------------------------------------还真没有想过。先去上课,回来再说。