Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1905015
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Java

2009-12-17 18:27:33

/**
 * 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
 *
 * @author chouy
 */

public class XunLeiExam2
{
    public static int i;
    
    public static void main(String[] args)
    {
        Thread th1 = new XLThread(1);
        th1.start();
        Thread th2 = new XLThread(2);
        th2.start();
        Thread th3 = new XLThread(3);
        th3.start();
        i = 1;
    }
    
    public static void addI()
    {
        if (i == 3)
            i = 1;
        else
            i = i + 1;
    }
}

class XLThread extends Thread
{
    public int v;

    public XLThread(int v)
    {
        this.v = v;
    }
    
    @Override
    public void run()
    {
        for (int i = 0; i < 10; i++)
        {
            while (true)
            {
                if (XunLeiExam2.i == this.v)
                {
                    System.out.print((char) ('A' + v - 1));
                    XunLeiExam2.addI();
                    break;
                }
                else
                    try
                    {
                        Thread.sleep(v);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
            }
        }
    }
}


阅读(1040) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~