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

📄 pcfix.java

📁 java 数据结构的一些简单实例和一些线程的应用主要是面向初学者
💻 JAVA
字号:
class Q
{
	int n;
	boolean valueSet=false;
	
	synchronized int get()
	{
		if(!valueSet)
			try
			{
				wait();
			}
			catch(InterruptedException e)
			{
				System.out.println("InterruptedException caught");
			}
		System.out.println("Got:"+n);
		valueSet=false;
		notify();
		return n;
	}
	
	synchronized void put(int n)
	{
		if(valueSet)
		try
		{
				wait();
		}
		catch(InterruptedException e)
		{
			System.out.println("InterruptedException caught");
		}
		this.n=n;
		valueSet=true;
		System.out.println("Put:"+n);
		notify();
	}
}

class Producer implements Runnable
{
	Q q;
	
	Producer(Q q)
	{
		this.q=q;
		new Thread(this,"Producer").start();
	}
	
	public void run()
	{
		int i=0;
		while(true)
		{
			q.put(i++);
		}
	}
}

class Consumer implements Runnable
{
	Q q;
	
	Consumer(Q q)
	{
		this.q=q;
		new Thread(this,"Consumer").start();
	}
	
	public void run()
	{
		while(true)
		{
			q.get();
		}
	}
	
}

public class PCFix 
{
    public static void main(String[] args)
    {
    	Q q=new Q();
    	new Producer(q);
    	new Consumer(q);
    	System.out.println("press control-c to stop");
    }
}

⌨️ 快捷键说明

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