⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example10.java

📁 创建线程
💻 JAVA
字号:
class ShouPiaoYuan
{
	int n_5=2, n_20=0;
	String s=null;
    public synchronized void rules(int money)
    {
    	if(money==5)    //如果使用该方法的线程传递的参数是5,就不用等待。
    	{
			System.out.println("A man with 5$ in hand!");
    		n_5=n_5+1;
    		s="给您入场券"+"  您的钱正好";
    		System.out.println("\n"+s);
    	}
    	else if(money ==20)
    	{
    		while(n_5<3)
    		{
				System.out.println("A man with 20$ in hand, but waiting for no changes!");
    			try{wait();}  //如果使用该方法的线程传递的参数是20须等待。
                catch(InterruptedException e){}
            }
            n_5=n_5-3;
            s="给您入场券"+"  您给我20,找您15元";
            System.out.println("\n"+s);
        }
        notifyAll();

    }
}
public class Example10 implements Runnable
{
	ShouPiaoYuan MissWang;
    Thread Zhang,Li;        //创建两个线程。
    public static void main(String[] args)
    {
    	(new Example10()).go();
    }
    void go(){
		Zhang=new Thread(this);
    	Li=new Thread(this);
        MissWang=new ShouPiaoYuan();
       	Zhang.start();
    	Li.start();

	}
    public void run()
    {
    	if(Thread.currentThread()==Zhang)
    	{
    		MissWang.rules(20);

        }
        else if(Thread.currentThread()==Li)
        {

        	MissWang.rules(5);

        }
    }
}

⌨️ 快捷键说明

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