你的put和get方法用同步会锁死的
class active 应该这样
class action
{
boolean lb_valueset = true;
int m_value;
action()
{
System.out.println("action build");
}
void put(int n)
{
synchronized(this){
if(!lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
m_value = n;
System.out.println("put " + m_value);
synchronized(this){
lb_valueset = false;
notify();
}
}
void get()
{
synchronized(this){
if(lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
System.out.println("get " + m_value);
synchronized(this){
lb_valueset = true;
notify();
}
}
}还有你的main里面的join()可用可不用
class active 应该这样
class action
{
boolean lb_valueset = true;
int m_value;
action()
{
System.out.println("action build");
}
void put(int n)
{
synchronized(this){
if(!lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
m_value = n;
System.out.println("put " + m_value);
synchronized(this){
lb_valueset = false;
notify();
}
}
void get()
{
synchronized(this){
if(lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
System.out.println("get " + m_value);
synchronized(this){
lb_valueset = true;
notify();
}
}
}还有你的main里面的join()可用可不用
synchronized void put(int n)
{
if(lb_valueset)
{
try
{
wait(); ..........lb_valueset是true,遇到wait(),线程就是开始等待,并且后面的语句是不会执行了的,
并且放开锁,线程开始运行get()但是了这时候lb_valueset还是true,所以put()的if
里面不会运行,也就没有notify()通知put()里面的wait()这样将一直的等待过程中
/*
* @(#)Welcome.java
* Have classes:Welcome
* Copyright 2005 Fane. All rights reserved.
* @author Fane
* @version 1.0, 30/03/05
* @since 1.0
*/
package person.fane.test;
class Action
{
boolean lb_valueset = false;
int m_value = 0;
int roundCount =0;
Action()
{
System.out.println("Action build");
}
synchronized void put(int n)
{
if(lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}else
{
m_value = n;
System.out.println("put " + m_value);
lb_valueset = true;
notify();
}
}
synchronized void get()
{
if(!lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}else
{
System.out.println("get " + m_value);
lb_valueset = false;
notify();
}
}
}class Producter implements Runnable
{
//public Thread t;
Action myAction;
Producter(Action temp)
{
myAction = temp;
//t = new Thread(this);
//t.start();
}
public void run()
{
while(true)
{
myAction.put(myAction.roundCount);
myAction.roundCount++;
if( myAction.roundCount > 20)
{
break;
}
}
}}
class Customer implements Runnable
{
//Thread t;
Action myAction;
Customer(Action temp)
{
//t = new Thread(this);
myAction = temp;
//t.start();
}
public void run()
{
while(true)
{
myAction.get();
if (myAction.roundCount > 20)
{
break;
}
}
}
}public class Welcome {
public static void main(String argc[])
{
Action myAction = new Action();
//Producter pro = new Producter(myAction);
//Customer cus = new Customer(myAction);
new Thread(new Producter(myAction)).start();
new Thread(new Customer(myAction)).start();
/*
try
{
pro.t.join();
cus.t.join();
}
catch(InterruptedException e)
{
System.out.println(e);
}
*/
System.out.println("main thread exit");
}
}
这里你的程序有几个问题,提醒一下:
1:没有遵守类的命名规范,首字母要大写
2:该方法体
if(lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}}else <==需添加,否则下面语句没执行机会
{
m_value = n;
System.out.println("put " + m_value);
lb_valueset = false;
notify();
}3:int roundCount =0; 我把你在其他两个类里的控制参数i改成roundCount ,定义成方法类的属性,这样可以在多个线程间共享
4:你线程从get开始,修改lb_valueset=false;使它从put开始
5:synchronized void get()
{
if(!lb_valueset) <==条件为真时
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("get " + m_value);
lb_valueset = true; 《=这里才能执行,而你还要把它设为真,肯定是会一直循环的,要改成lb_valueset = false;
notify();
}
}6:线程的启动不要在构造方法中,
producter(action temp)
{
act = temp;
t = new Thread(this);
t.start();
}
这样做很不可取,改成在main方法里启动
class Action
{
boolean lb_valueset = false;
int m_value = 0;
int roundCount =0;
Action()
{
System.out.println("Action build");
}
synchronized void put(int n)
{
if(lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}else
{
m_value = n;
System.out.println("put " + m_value);
roundCount++; <==这里添加上
lb_valueset = true;
notify();
}
}
synchronized void get()
{
if(!lb_valueset)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}else
{
System.out.println("get " + m_value);
lb_valueset = false;
notify();
}
}
}............ //myAction.roundCount++; <==这句注释掉
否则因为线程切换过快,++为被执行就切换线程,导致输出有误