确定JAVA中持久的线程 [英] Determine long lasting thread in JAVA

查看:94
本文介绍了确定JAVA中持久的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个确定持久线程的程序。有5个具有不同优先级的线程,并且两个具有最高优先级的线程保持睡眠状态.AlAlive()方法用于检查线程是否存活并且基于该确定持久线程,但它在无限运行循环而不是给持久的线程......!下面是代码

-------------------------------------- ----------------------------

I developing a program which determine the long lasting thread. There are 5 threads with different priorities and two threads of highest priorities are kept to sleep.Also isAlive() method is used to check whether the thread is alive or not and based on that the long lasting thread is determined,but it runs in infinite loop instead of giving long lasting thread...! below is the code
------------------------------------------------------------------

import java.util.*;
class ThreadDemo extends Thread
{
    Thread t;
    String name;
    int priority;

    ThreadDemo(int priority,String name)
    {
        this.name=name;
        this.priority=priority;
        t=new Thread(this,name);
        System.out.println("New Thread "+t);
        t.start();
    }
    public void run()
    {
            int p=t.getPriority();
            try
            {
                if(p==9 || p==8)
                {
                    t.sleep(2000);
                }
                else
                {
                    System.out.println(t.currentThread()+"is alive?"+t.isAlive());
                }
                while(t.isAlive())
                {
                    System.out.println("Long lasting thread:"+t.getName());
                }

            }
            catch(InterruptedException ie)
            {
                System.out.println("Thread Interrupted...!");
            }

    }
}
class MainThread
{
    public static void main(String args[])
    {
        //ThreadDemo a,b,c,d,e;
        new ThreadDemo(5,"One");
        new ThreadDemo(6,"Two");
        new ThreadDemo(7,"Three");
        new ThreadDemo(8,"Four");
        new ThreadDemo(9,"Five");

    }
}

推荐答案

观察到的beaviour没有什么奇怪的:你明确地说优先级不同于 8 9 的线程,无限循环。
There is nothing strange in the observed beaviour: you explicitely put threads with priorities different from 8 and 9 in a infinite pauseless loop.


这篇关于确定JAVA中持久的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆