java代码实现取放 java怎么实现
用Java代码模拟实现:一个人不断往箱子里放苹果,另一个人不断从箱子里取苹果,箱子里只能放5个苹
package com.zt.test;
创新互联-专业网站定制、快速模板网站建设、高性价比海北州网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式海北州网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖海北州地区。费用合理售后完善,十余年实体公司更值得信赖。
import java.util.Stack;
public class Test7 {
private StackString box;
public String operationBox(boolean flag, String num) {
String str = "";
synchronized (Test7.class) {
if (flag) {
this.box.push(num);
} else {
str = this.box.pop();
}
}
return str;
}
public static void main(String args[]) {
Test7 a = new Test7();
a.initApples();
}
public Test7() {
this.box = new StackString();
}
public void forSleep(int i) {
try {
Thread.sleep(i);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 放苹果的人
*/
public void putApple() {
int no = 1;
while (true) {
this.forSleep(1000);
int check = this.box.size();
System.out.println("---------现在有" + check + "个苹果---------");
if (check = 5) {
System.out.println("------------箱子里有5个苹果无法放入------------");
continue;
} else {
System.out
.println("------------放入第" + no + "个苹果--------------");
this.operationBox(true, no + "");
no = no + 1;
}
}
}
/**
* 拿苹果的人
*/
public void getApple() {
while (true) {
this.forSleep(800);
int check = this.box.size();
System.out.println("=========现在有" + check + "个苹果============");
if (check == 0) {
System.out.println("==========箱子里有0个苹果无法去除===========");
continue;
} else {
String str = this.operationBox(false, null);
System.out.println("==========从箱子出取出第" + str + "个苹果==========");
}
}
}
/*
* 初始化两个人
*/
public void initApples() {
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
putApple();
} catch (Exception e) {
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
getApple();
} catch (Exception e) {
}
}
});
t2.start();
}
}
一个放一个拿,operationBox()锁住stack的操作,forsleep方法是进行休眠的,模拟一块一慢,如果需要无序时间,自己写随机函数进行线程休眠。
有这样一个需求,若缓存中有数据直接从中取,否则从数据库中取出数据并放入缓存中,用java怎么实现啊?
每次请求参数或者sql作为缓存key,如果key存在从缓存取,否则走数据库,结果缓存.缓存框架选择因业务而异
java代码 30分 呵呵
获取页面数据存放到本地数据库,用java实现
这种的建议用htmlcleaner的类包解析
直接就是java对象
public class URLReader {
public String readResponseFromURL(String url) throws MalformedURLException, IOException {
// open reader for URL
InputStreamReader in = new InputStreamReader(new URL(url).openStream());
// read contents into string buffer
StringBuilder input = new StringBuilder();
int ch;
while ((ch = in.read()) != -1) {
input.append((char) ch);
}
return input.toString();
}
}
调用
URLReader reader = new URLReader();
String input = reader.readResponseFromURL(url);
TagNode node = htmlCleaner.clean(input);
这里node就是html的节点了
所有的都可以从里面取得
用Java实现ATM的转账,取款,改密码,查询功能, 要代码!
import javax.swing.JOptionPane;
public class Account
{
private String name;
private String account;
private String data;
private String ID;
private double balance;
public Account(String name,double balance,String data,String ID)
{
this.name = name;
this.balance = balance;
this.data=data;
this.ID=ID;
}
public String getaccount()
{
this.account=String.valueOf((int)(Math.random()*100000)+1);
return account;
}
public String getdata()
{
return data;
}
public String getID()
{
return ID;
}
public String getName()
{
return name;
}
public double getbalance()
{
return balance;
}
//查看账户余额
public double balance()
{
return balance;
}
//查看开户时间
public String data()
{
return data;
}
//存款操作
public boolean put(double value)
{
if (value0)
{
this.balance += value;
return true;
}
return false;
}
//取款操作
public double get(double value)
{
if (value0)
{
if (value=this.balance)
this.balance -= value;
else
{
value = this.balance;
this.balance = 0;
}
return value;
}
return 0;
}
public static void main(String args[]){
Account user=new Account("张三",2000,"2011/05/23","362329198906234225");
String str;
int b;
String s=JOptionPane.showInputDialog("您好,存款选择1,取款选择2,退出选择0");
int x=Integer.parseInt(s);
while(x!=0){
if(x==1)
{
str=JOptionPane.showInputDialog("输入您要存入的数额");
b=Integer.parseInt(str);
if( user.put(b)){
JOptionPane.showMessageDialog(null, "请放入钞票!");
JOptionPane.showMessageDialog(null,"开户账号为"+user.getaccount()+"\n"+user.getName()+"\n开户时间为"+user.data()+"\n您的余额为"+user.balance());
}
else
JOptionPane.showMessageDialog(null, "你所输入的存款数额有误!");
}
else{
str=JOptionPane.showInputDialog("输入您要取出的数额");
b=Integer.parseInt(str);
if(buser.balance())
{
JOptionPane.showMessageDialog(null, "余额不足");
}
else
{
JOptionPane.showMessageDialog(null, "请取出钞票!");
user.get(b);
JOptionPane.showMessageDialog(null,"开户账号为"+user.getaccount()+"\n"+user.getName()+"\n开户时间为"+user.data()+"\n您的余额为"+user.balance());
}
}
s=JOptionPane.showInputDialog("您好,存款选择1,取款选择2,退出选择0");
x=Integer.parseInt(s);
}
}
}
当前题目:java代码实现取放 java怎么实现
文章分享:http://myzitong.com/article/ddcgiso.html