thread10.java

来自「学习java编程的好程序」· Java 代码 · 共 44 行

JAVA
44
字号
public class thread10
{
    public static void main(String[] args)
    {
        compute t=new compute('a');
        compute t1=new compute('b');
        compute t2=new compute('c');
        t.start();
        t1.start();
        t2.start();
    }
}
///创建一个线程类
///在这线程类中,使用循环语句输出字符
///在run方法中,使用同步块来给线程加一把锁
class compute extends Thread  
{
    char ch;
    static Object obj=new Object();
    compute(char ch)
    {
        this.ch=ch;
    }
    public void print(char ch)
    {
        for(int i=1;i<10;i++)
        {
            System.out.print(ch);
        }
    }
	
    public void run()
    {
        synchronized(obj)
        {
            for(int i=1;i<10;i++)
            {
                print(ch);
                System.out.println();
            }
        }	
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?