public class SrThread2
{
public static void main(String args[])
{
SubThread another=new SubThread();
another.start();
while(another.isAlive()) //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立
//非常奇怪.
{
try{Thread.sleep(100);} //这个Thread为什么一定指的是主线程,
//它只是一个类名
catch(InterruptedException e){return;}
System.out.println("mainthread is running");
}
}
}
class SubThread extends Thread
{
public void run()
{
for (int i=0;i<3;i++)
{
System.out.println(this.getName()+" is running");
}
}
}
{
public static void main(String args[])
{
SubThread another=new SubThread();
another.start();
while(another.isAlive()) //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立
//非常奇怪.
{
try{Thread.sleep(100);} //这个Thread为什么一定指的是主线程,
//它只是一个类名
catch(InterruptedException e){return;}
System.out.println("mainthread is running");
}
}
}
class SubThread extends Thread
{
public void run()
{
for (int i=0;i<3;i++)
{
System.out.println(this.getName()+" is running");
}
}
}
解决方案 »
- 判断字符串是不是“INSERT”、“DELETE”、“UPDATE”或“SELECT” SQL语句的正则表达式怎么写?
- 比较两者(eg:字符串、引用类型、基本类型)是否相等的方法有哪些呢?
- 100分求助此页面实现代码,后台已经写好了,主要是前台展现。
- 如何解决客户端发送数据后连接自动断开的问题
- JTextArea中用getText()方法,怎么抛出exception?
- 请教:关于AWT一个奇怪的问题
- 急:高分求助关于jdbc连接MySQL数据库的问题.
- 如何改变java 的版本?
- java中如何表示time的类型,用那个包中的那个类?
- 求一个swing界面代码,单击一次按钮可以添加一个小面板
- 13个就餐问题,拿刀拿叉问题,大家碰到过没,不知道谁能用JAVA来描述。
- 我是JAVA新手,问个白痴的基础题,希望大家帮忙解答下,不胜感激
while(another.isAlive()) //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立
//非常奇怪.
{
//在这加一句输出,看看最后的输出情况
System.out.println(this.getName()+" is running");
try{Thread.sleep(100);} //这个Thread为什么一定指的是主线程,
//它只是一个类名
catch(InterruptedException e){return;}
System.out.println("mainthread is running");
}
2.去看sleep方法的文档,
sleep
public static void sleep(long millis,
int nanos)
throws InterruptedException在指定的毫秒数加指定的纳秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。
让当前线程休眠,你放在main线程里当然休眠的是main了,你把
try{Thread.sleep(100);} //这个Thread为什么一定指的是主线程,
//它只是一个类名
catch(InterruptedException e){return;}
放在另一个线程的main方法里休眠的就是另一个线程了.结贴吧.
在哪个线程中调用Thread.sleep()函数就是指的哪个线程,你这里是Main函数调用的,Thread当然就是指的主线程了。如果你是在类SubThread的run()函数中加入了Thread.sleep()函数调用这个Thread就是指导的SubThread进程了。其实这个说法还有些问题,sleep()方法是Thread类的静态方法,public static void sleep(long millis) throws InterruptedException在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。
another.start();
while(another.isAlive()) //线程已经全部执行完了,已经dead了,为什么这个条件仍然成立 another.start(); 只是启动了一个SubThread线程而已,它并不会一直等到SubThread线程执行完毕之后才往下执行下一条while(another.isAlive())指令,在Java的多线程机制中,主线程和子线程是交替执行的。所以整个运行过程中main线程和SubThread子线程是交替执行的,在SubThread没有执行完毕的时候main线程也可以接着往下执行。
这是本人的一点见解,不知对楼主可有帮助。如果不当之处也请各位兄台批评指正。
//在这加一句输出,看看最后的输出情况
System.out.println(this.getName()+" is running");
加不了this啊!
我太笨了,不好意思.
你上个帖子结了,发到这吧,本贴名为再论,原论还没结贴呢。public class Maintest {
public static void main(String args[]) throws Exception {
ThreadDemo thr = new ThreadDemo();
thr.start();
for (int i = 1; i < 1000; i++) {
synchronized (Maintest.class) {
System.out.println("main: " + i);
Maintest.class.notify();
Maintest.class.wait();
}
} }
}class ThreadDemo extends Thread {
public void run() {
for (int i = 1; i < 1000; i++) {
try {
synchronized (Maintest.class) {
System.out.println(this.getName() + ":" + i);
Maintest.class.notify();
Maintest.class.wait();//会释放锁
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//它只是一个类名
多线程的main线程里这样已经说明。
它已经进入这个条件判断句了。
所以不是dead状态的时候才跳入的。
main的优先级为5
gc()的优先级最低,所以收回内存并不是程序员能马上控制的
线程一旦创建,那么享受的待遇都是一样的(没优先级和后台情况下),
谁规定一只猪死了,由此猪生下的小猪就得跟着死了
java线程中没有 正在运行的线和程可运行的线程之分别,它们统一是alive。
public class SrThread2
{
public static void main(String args[])
{
SubThread another=new SubThread();
another.start();
while(another.isAlive()) //another线程还没有运行,因为another是可运行线程,所以another.isAlive()返回true.
{
try{Thread.sleep(100);} //这个Thread不一定指的是主线程,它指的是当前线程。
//当前线程sleep后,计算机调雅another线程运行。
catch(InterruptedException e){return;}
System.out.println("mainthread is running");
}
}
}
class SubThread extends Thread
{
public void run()
{
for (int i=0;i<3;i++)
{
System.out.println(this.getName()+" is running");
}
}
}