您的位置:首页 >> 编程开发 >> Java >> J2SE >> 正文
J2SE RSS
 

Java之多线程(1)

http://www.rdxx.com 05年07月24日 21:14 JR 我要投稿

关键词: 多线程 , Java , 线程

InterruptCheck.java
public class InterruptCheck extends Object {
        public static void main(String[] args) {
                Thread t = Thread.currentThread();
                System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
                t.interrupt();
                System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
                System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());

                try {
                        Thread.sleep(2000);
                        System.out.println("was NOT interrupted");
                } catch ( InterruptedException x ) {
                        System.out.println("was interrupted");
                }
                //在这里因为sleep抛出了异常,所以它清除了中断标志

                System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
        }
}

class ThreadTest implements Runnable
{
    private int tickets = 100 ;
    public void run()
    {
        while(true)
        {
            //假如一个线程未执行完,而另一线程又运行此代码,则会出现不同步问题
            //为说明问题,将线程延迟
            if(tickets>0)
            {

9 7 3 1 2 3 4 5 6 7 8 9 10 4 8 :


 
 
标签: 多线程 , Java , 线程 打印本文
 
 
  相关资讯
RSS
 
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站