菜鸟学java,帮忙做三道题

zhaodiqing 2013-02-19 04:44:02
1.个人账户里存有1000千块,一个人用卡取500,另一个人用存折取800,也就是同时有两个线程对同一个实例操作,请正确输出操作后的结果。


2.有一个Map<Long, Set<String>>容器,要求设计一个同步操作:一个线程每隔1秒往Map中put一个键值对,另一个线程每隔2秒从中移除一个键值对,还有一个线程每隔3秒迭代打印所有的键值对内容以及Set中的内容。


3.某自动售票机有几个买票的操作平台,机里有20张票,一张卖5元,现在里面没现金,只有票,售票机只能放入10元跟5元。张三用10元来买一张票,售票机找不出零钱,需要等待,此时李四用五元来买一张票,售票机就可以找钱了。
...全文
857 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
sca4441479 2013-02-28
  • 打赏
  • 举报
回复
最后一题 是啥意思
zhaodiqing 2013-02-26
  • 打赏
  • 举报
回复
引用 15 楼 haoyu01 的回复:
第二题: 实现了一个程序 insertThread每秒在尾部插入一个数据 deleteThread每2秒删除第一个 printThread每三秒打印一次 插入的数据是 id: 2个数字 数据中字符串楼主可以自己改~~*_* 希望对楼主有帮助~ import java.util.ArrayList; import java.util.HashMap;……
非常感谢,学习了。 但如下这然话 编译不了: Object[] strs = myMap.get(keys[(int) i]).toArray(); 还有一题可以做下吗
赏金--猎人 2013-02-25
  • 打赏
  • 举报
回复
楼上的程序写的挺好的,lz可以好好学习一下!
haoyu01 2013-02-23
  • 打赏
  • 举报
回复
第二题: 实现了一个程序 insertThread每秒在尾部插入一个数据 deleteThread每2秒删除第一个 printThread每三秒打印一次 插入的数据是 id: 2个数字 数据中字符串楼主可以自己改~~*_* 希望对楼主有帮助~ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; //有一个Map<Long, Set<String>>容器,要求设计一个同步操作: //一个线程每隔1秒往Map中put一个键值对, //另一个线程每隔2秒从中移除一个键值对, //还有一个线程每隔3秒迭代打印所有的键值对内容以及Set中的内容。 public class SynchronousMap { Map<Long,Set<String>> map; public SynchronousMap(){ this.map = new HashMap<Long,Set<String>>(); } public Map<Long,Set<String>> getMap(){ return map; } public static void main(String[] args){ SynchronousMap map = new SynchronousMap(); insertThread thread1 = new insertThread(map); deleteThread thread2 = new deleteThread(map); printThread thread3 = new printThread(map); thread1.start(); thread2.start(); thread3.start(); } } class insertThread extends Thread { Map<Long,Set<String>> myMap; insertThread(SynchronousMap MyMap){ myMap = MyMap.getMap(); } public void run() { while (true) { synchronized (myMap) { long num; if(myMap.size() == 0) num = 0; else num = (Long) myMap.keySet().toArray()[myMap.keySet().size()-1]; System.out.print("insert\n\n"); Set<String> strlst = new HashSet<String>(); strlst.add(num + 1 + ""); strlst.add(num + 2 + ""); myMap.put(num + 1, strlst); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class deleteThread extends Thread { Map<Long,Set<String>> myMap; deleteThread(SynchronousMap MyMap){ myMap = MyMap.getMap(); } public void run() { while (true) { synchronized (myMap) { System.out.print("delete\n\n"); if (myMap.size() >= 1){ Set<String> s = myMap.remove(myMap.keySet().toArray()[0]); if(s == null) System.out.println("ssss"); } } try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class printThread extends Thread { Map<Long,Set<String>> myMap; printThread(SynchronousMap MyMap){ myMap = MyMap.getMap(); } public void run() { while (true) { synchronized (myMap) { System.out.print("print:\n"); Object[] keys = (Object[]) myMap.keySet().toArray(); for (long i = 0; i < keys.length; i++) { Object[] strs = myMap.get(keys[(int) i]).toArray(); System.out.print(keys[(int) i] + ":"); for (int j = 0; j < strs.length; j++) { System.out.print(strs[j].toString() + " "); } System.out.print("\n"); } System.out.print("\n"); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
zhaodiqing 2013-02-23
  • 打赏
  • 举报
回复
第一题已做完,求后两题。。。
zhaodiqing 2013-02-22
  • 打赏
  • 举报
回复
引用 9 楼 szx_zsx 的回复:
就是多线程加锁的问题!
能帮忙写一个吗
赏金--猎人 2013-02-22
  • 打赏
  • 举报
回复
就是多线程加锁的问题!
飘飘哥 2013-02-22
  • 打赏
  • 举报
回复
第一题线程问题 你可以参考http://blog.csdn.net/xu_201205/article/details/8050941
泰坦小毛驴 2013-02-22
  • 打赏
  • 举报
回复
都是线程问题,现在没时间写,搜索个,扒扒得了
zhaodiqing 2013-02-22
  • 打赏
  • 举报
回复
顶,请解决。。。。。
Javarookie 2013-02-21
  • 打赏
  • 举报
回复
这不是线程同步吗?
zhaodiqing 2013-02-21
  • 打赏
  • 举报
回复
后面几题也请做下
失落夏天 2013-02-20
  • 打赏
  • 举报
回复
以前写的一个例子,和第一个问题很像,楼主看看吧。 后面的都涉及线程同步问题,原理基本一样,就不细说了。 账户:

package com.briup.ch10;

public class Account {
	private long no;
	private double balance;
	
	synchronized public String deposit(double amount){
		String msg="";

		if(amount<0){
			msg="sorry,amount not right.";
		}else{
			balance=balance+amount;
			msg=Thread.currentThread().getName()+" earn"+amount+" and balance has"+balance;
			notifyAll();
		}		
		return msg;
	}
	synchronized public String withdraw(double amount){
		String msg="";

		while(amount<0||amount>balance){
			msg="sorry,amount not right.";
			try{
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
			balance=balance-amount;
			msg=Thread.currentThread().getName()+" withdraw"+amount+" and balance has"+balance;
		return msg;
	}
}
存钱的

package com.briup.ch10;

public class Boys extends Thread{
	private Account account;
	
	public Boys(Account account,String name){
		this.account=account;
		setName(name);
		start();
	}
	public void run(){
		while(true){
			long amount=(long)(Math.random()*10000);
			String msg=account.deposit(amount);
			System.out.println(msg);
			try{
				sleep(amount);
			}catch (InterruptedException e){
				e.printStackTrace();
			}
			
	}
}
}
取钱的
package com.briup.ch10;

public class Girls implements Runnable{
	private Account account;
	public Girls(Account account,String name){
		Thread t=new Thread(this);
		this.account=account;
		t.setName(name);
		t.start();
	}
	public void run(){
		while(true){
			long amount=(long)(Math.random()*10000);
			String msg=account.withdraw(amount);
			System.out.println(msg);
			try{
				Thread.currentThread().sleep(amount);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
}
测试

package com.briup.ch10;

public class ATM{
	public static void main(String[] args) {
		Account account=new Account();
		Thread t1=new Boys(account,"tony");
		new Girls(account,"lucy");
		new Girls(account,"track");
	}
}
zhaodiqing 2013-02-20
  • 打赏
  • 举报
回复
没人回复下。。。
zhaodiqing 2013-02-19
  • 打赏
  • 举报
回复
顶。。。。。。。
zhaodiqing 2013-02-19
  • 打赏
  • 举报
回复
借记卡; 明天要用!
bigbro001 2013-02-19
  • 打赏
  • 举报
回复
楼主什么时候要,晚上回去可以做给你
healer_kx 2013-02-19
  • 打赏
  • 举报
回复
1,账户是借记卡还是信用卡? 2,如果都是线程的问题,那么和第一题没啥本质区别啊。

62,614

社区成员

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

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