62,612
社区成员
发帖
与我相关
我的任务
分享
package Thread;
public class exchange1
{
private int number;
public synchronized void decrease()
{
while(0 != number)
{
try
{
wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
number++;
notifyAll();
}
}
public synchronized void increase()
{
while(0 == number)
{
try
{
wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
number--;
notifyAll();
}
}
}
[/quote]你尝试着把我的代码调试下,notifyall我也试过,没有作用
package Thread;
public class exchange1
{
private int number;
public synchronized void decrease()
{
while(0 != number)
{
try
{
wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
number++;
notifyAll();
}
}
public synchronized void increase()
{
while(0 == number)
{
try
{
wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
number--;
notifyAll();
}
}
}
public class CarObj {
private Integer speedInteger = 0; // 速度
private Integer juliInteger = 0; // 距离
private Integer timeInteger = 0;// 时间
public CarObj() {
}
/**
*
* @param speed 米/秒
* @param juli 米
* @param time 秒
*/
public CarObj(Integer speed, Integer juli, Integer time) {
speedInteger = speed ;
juliInteger = juli ;
timeInteger = time ;
}
/*
* 对对象进行封装
*
*/
public Integer getSpeedInteger() {
return speedInteger;
}
public void setSpeedInteger(Integer speedInteger) {
this.speedInteger = speedInteger;
}
public Integer getJuliInteger() {
return juliInteger;
}
public void setJuliInteger(Integer juliInteger) {
this.juliInteger = juliInteger;
}
public Integer getTimeInteger() {
return timeInteger;
}
public void setTimeInteger(Integer timeInteger) {
this.timeInteger = timeInteger;
}
}
public class CheThreadExt implements Runnable {
private String nameString;
public CheThreadExt(String name) {
this.nameString = name;
}
public void run() {
int i = 0 ;
while (i < Constant.allTimeInteger) { //时间 秒
try {
Thread.currentThread().sleep(1000);
CarObj carObj = CarSynTable.getOneCar(nameString);
carObj.setTimeInteger(carObj.getTimeInteger() + 1);
carObj.setJuliInteger(carObj.getJuliInteger()
+ carObj.getSpeedInteger());
CarSynTable.putOneCar(nameString, carObj) ;
i ++ ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 代码执行
*
* @param args
*/
public static void main(String[] args) {
CheThreadExt mx = new CheThreadExt("Car_A");
CheThreadExt mx1 = new CheThreadExt("Car_B");
CheThreadExt mx2 = new CheThreadExt("Car_C");
new Thread(mx,"Car_A").start();
new Thread(mx1,"Car_B").start();
new Thread(mx2,"Car_C").start();
for(int i = 0 ; i < 10 ; i ++){
try {
Thread.currentThread().sleep(5000);
CarSynTable.printCurCarsInfo() ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(0);
}
}
public class RandomUtil {
private static final Random random = new Random();
public static synchronized int getSpeed() {
return random.nextInt(50); // 随机数范围
}
public static void main(String args[]) {
for(int i = 0 ; i < 100 ; i ++){
System.out.println("getSpeed() : " + getSpeed());
}
}
}
public class Constant {
public static Integer allTimeInteger = 1000 ; //总共行走时间
}
public class CarSynTable {
private static HashMap<String, CarObj> carCurStatus = new HashMap<String, CarObj>();
public static synchronized void putOneCar(String thredNm, CarObj carobj) {
carCurStatus.put(thredNm, carobj);
}
public static synchronized CarObj getOneCar(String thredNm) {
if(null == carCurStatus.get(thredNm)){
return new CarObj(RandomUtil.getSpeed(), 0, 0);
}
return carCurStatus.get(thredNm);
}
public static synchronized HashMap<String, CarObj> getCarCurStatus() {
return carCurStatus;
}
public static synchronized void printCurCarsInfo() {
Set set = getCarCurStatus().entrySet();
Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
for (int i = 0; i < entries.length; i++) {
CarObj carobj = (CarObj) entries[i].getValue();
System.out.println("CarName : " + entries[i].getKey().toString()
+ ", speed : " + carobj.getSpeedInteger() + ", time : "
+ carobj.getTimeInteger() + " , juli : "
+ carobj.getJuliInteger() + " !");
}
}
}
这个是重新贴的,前面的很抱歉,第一次发帖,没注意规范,我就想问下,如何实现线程与线程之间的通信。
public class exchange1
{
private int number;
public synchronized void decrease()
{
while(0 != number)
{
try
{
wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
number--;
notify();
}
}
public synchronized void increase()
{
while(0 == number)
{
try
{
wait();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
number++;
notify();
}
}
}
比如这一部分代码,如何增加到原有的程序之中,我试着添加之后发现,有一个线程一直处于wait()之中,没有被唤醒。