分享一个关于多线程的程序!!!

娟流韶华年 2016-04-23 10:48:07
线程类部分:
public class CheThreadExt extends Thread {

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 thread1 = new CheThreadExt("Car_A");

CheThreadExt thread2 = new CheThreadExt("Car_B");

CheThreadExt thread3 = new CheThreadExt("Car_C");

thread1.start();

thread2.start();

thread3.start();

for(int i = 0 ; i < 10 ; i ++){

try {
Thread.currentThread().sleep(5000);

CarSynTable.printCurCarsInfo() ;

} catch (InterruptedException e) {
e.printStackTrace();
}

}

System.exit(0);

}
}
HashMap以及对象初始化部分:

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 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;
}

}
...全文
258 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangxheng 2016-04-28
  • 打赏
  • 举报
回复
线程间通信,可以用三方全局变量来达到效果。 有事的一方,主动把要求写到第三方变量去。 其它方不断的查看第三方变量,看到变化,再判断是不是要自己处理。 注意事项 1、第三方变量的同步性。 2、事情的处理会不会忙不过要求,导致要求爆棚
娟流韶华年 2016-04-26
  • 打赏
  • 举报
回复
引用 9楼Java-我去 的回复:
[quote=引用 8 楼 u013709217 的回复:] [quote=引用 6 楼 zsw12013 的回复:] [quote=引用 4 楼 u013709217 的回复:] [quote=引用 2 楼 zsw12013 的回复:] 又看见一个不回提问题的人
抱歉抱歉,第一次发帖。 可以指导一下我妈,谢谢了[/quote] 不好意思有点事情,回复晚了 可以看看这个 http://bbs.csdn.net/topics/390335073[/quote] 我主要的需求是能够实现这三个线程之间的通信,不是有那个wait()和notify()方法用来实现这个功能吗,我自己添加之后发现能够运行程序,但是实际运行出来之后,有一个线程不能被唤醒,就想问下是怎么一回事,代码都贴出来了,最后一部分exchange1类的代码就是关于线程相互通信的,你帮我看看有什么问题[/quote] 抱歉最近比较忙 1、在多线程环境中HashMap 是线程不安全的,可以用ConcurrentHashMap 2、造成阻塞的原因 exchange1 消费着生产者这儿写的有问题

  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我也试过,没有作用
Java-我去 2016-04-26
  • 打赏
  • 举报
回复
引用 8 楼 u013709217 的回复:
[quote=引用 6 楼 zsw12013 的回复:] [quote=引用 4 楼 u013709217 的回复:] [quote=引用 2 楼 zsw12013 的回复:] 又看见一个不回提问题的人
抱歉抱歉,第一次发帖。 可以指导一下我妈,谢谢了[/quote] 不好意思有点事情,回复晚了 可以看看这个 http://bbs.csdn.net/topics/390335073[/quote] 我主要的需求是能够实现这三个线程之间的通信,不是有那个wait()和notify()方法用来实现这个功能吗,我自己添加之后发现能够运行程序,但是实际运行出来之后,有一个线程不能被唤醒,就想问下是怎么一回事,代码都贴出来了,最后一部分exchange1类的代码就是关于线程相互通信的,你帮我看看有什么问题[/quote] 抱歉最近比较忙 1、在多线程环境中HashMap 是线程不安全的,可以用ConcurrentHashMap 2、造成阻塞的原因 exchange1 消费着生产者这儿写的有问题

  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();

		}
	}
}

Java-我去 2016-04-23
  • 打赏
  • 举报
回复
又看见一个不回提问题的人
娟流韶华年 2016-04-23
  • 打赏
  • 举报
回复
引用 6 楼 zsw12013 的回复:
[quote=引用 4 楼 u013709217 的回复:] [quote=引用 2 楼 zsw12013 的回复:] 又看见一个不回提问题的人
抱歉抱歉,第一次发帖。 可以指导一下我妈,谢谢了[/quote] 不好意思有点事情,回复晚了 可以看看这个 http://bbs.csdn.net/topics/390335073[/quote] 我主要的需求是能够实现这三个线程之间的通信,不是有那个wait()和notify()方法用来实现这个功能吗,我自己添加之后发现能够运行程序,但是实际运行出来之后,有一个线程不能被唤醒,就想问下是怎么一回事,代码都贴出来了,最后一部分exchange1类的代码就是关于线程相互通信的,你帮我看看有什么问题
娟流韶华年 2016-04-23
  • 打赏
  • 举报
回复
如果在这个程序的基础之上,加入一个线程之间相互通信的功能,应该如何去做?
Java-我去 2016-04-23
  • 打赏
  • 举报
回复
引用 5 楼 u013709217 的回复:
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()之中,没有被唤醒。
说的太少,不知道你的需求是啥
Java-我去 2016-04-23
  • 打赏
  • 举报
回复
引用 4 楼 u013709217 的回复:
[quote=引用 2 楼 zsw12013 的回复:] 又看见一个不回提问题的人
抱歉抱歉,第一次发帖。 可以指导一下我妈,谢谢了[/quote] 不好意思有点事情,回复晚了 可以看看这个 http://bbs.csdn.net/topics/390335073
娟流韶华年 2016-04-23
  • 打赏
  • 举报
回复
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()之中,没有被唤醒。
娟流韶华年 2016-04-23
  • 打赏
  • 举报
回复
引用 2 楼 zsw12013 的回复:
又看见一个不回提问题的人
抱歉抱歉,第一次发帖。 可以指导一下我妈,谢谢了
残月飞鹰 2016-04-23
  • 打赏
  • 举报
回复
没有注释的代码不愿意看

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧