Java 多线程1

来源:互联网 发布:java线程结束通知 编辑:程序博客网 时间:2024/05/21 08:58
public class MyThread  implements Runnable
{
    private int i = 100;
    
    public void run()
    {
        while(true)
        {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + i);
                i--;
                Thread.yield();
                if(i<0)
                {
                    break;
                }
            }

        }
    }

}


public class ThreadMain
{
    public static void main(String args[])
    {
        MyThread myThread = new MyThread();
        
        Thread t1 = new Thread(myThread);
        Thread t2 = new Thread(myThread);
        t1.setName("线程a  ");
        t2.setName("线程b  ");
        t1.start();
        t2.start();
        
    }
}

原创粉丝点击