我在下面的代碼中收到錯誤。有人能幫我嗎?生產者消費者沒有同步程序時出錯
class Q
{
int n;
synchronized int get()
{
System.out.println("Got n :"+n);
return n;
}
synchronized void put(int n)
{
this.n = n;
System.out.println("Put n :"+n);
}
}
class Producer implements Runnable
{
Q q1;
Producer(Q q)
{
this.q1 = q;
new Thread(this).start();
}
public void run()
{
int i =0;
q1.put(i++);
}
}
class Consumer implements Runnable
{
Q q1;
Consumer(Q q)
{
this.q1 = q;
new Thread(this).start();
}
public void run()
{
q1.get();
}
}
class ProducerConsumerWithoutSync
{
public static void main(String args[])
{
Q q = new Q();
new Producer(q);
new Consumer(q);
}
}
什麼是錯誤信息? – Jens
永遠不要告訴其他程序員,你不知道錯誤是什麼,你會得到一個錯誤! –
ProducerConsumerWithoutSync.java:7:缺少返回語句 } ^ 1錯誤 –