none applicable
我先建了一个project,然后建了一个class,但是为什么运行时点击“run/run as”下边出现none applicable而不是java application?程序没有问题的,是copy书上的代码!如下:
public class BufferLock //加互斥锁的缓冲区
{
private int value; //共享变量
private boolean isEmpty = true; //value是否为空的信号量
public synchronized void put(int i) //同步方法
{
while (!isEmpty) //当value不空时,等待
{
try
{
this.wait(); //使调用该方法的当前线程等待,即阻塞自己
}
catch(InterruptedException e) {}
}
value = i; //当value空时,value获得值
isEmpty = false; //设置value为不空状态
notify(); //唤醒其他等待线程
}
public synchronized int get() //同步方法
{
while (isEmpty) //当value空时,等待
{
try
{
this.wait();
}
catch(InterruptedException e) {}
}
isEmpty = true; //设置value为空状态,并返回值
notify();
return value;
}
}
class Sender extends Thread //发送线程类
{
private BufferLock buffer;
public Sender(BufferLock buffer)
{
this.buffer = buffer;
}
public void run( )
{
for (int i=1;i<6;i++)
{
buffer.put( i );
System.out.println("Sender put : " + i );
}
}
}
class Receiver extends Thread //接收线程类
{
private BufferLock buffer;
public Receiver(BufferLock buffer)
{
this.buffer = buffer ;
}
public void run()
{
for (int i=1; i<6;i++)
System.out.println("\t\t\tReceiver get : " + buffer.get()) ;
}
public static void main (String args[])
{
BufferLock buffer = new BufferLock();
(new Sender(buffer)).start();
(new Receiver(buffer)).start();
}
}