日期:2014-05-20 浏览次数:21425 次
1.写出下面代码的执行结果:
public class T {
public static void main(String[] args) {
Byte b=new Byte("127");
System.out.println(b.toString()==b.toString());
}
}
2.写出下面代码的执行结果:
package com.ibm.bmcc.test;
public class T {
public static void main(String[] args) {
method1();
System.out.println("f");
}
static void method1(){
try{
method2();
System.out.println("a");
}catch (NullPointerException e) {
System.out.println("b");
}finally{
System.out.println("c");
}
System.out.println("d");
}
static void method2(){
System.out.println("e");
throw new IllegalStateException();
}
}
3.用Java实现一个单利模式
4.多线程编程,第一个线程负责对某个数加一,第二个线程负责对这个数减一
public class BankQueueThread {
private static List<CustomerInfo> queue= Collections.synchronizedList(new ArrayList<CustomerInfo>());
private static List<String> vipqueue= Collections.synchronizedList(new ArrayList<String>());
private static int num=0;
private static int rnum=0;
private SimpleDateFormat datef=new SimpleDateFormat("HH:mm");
public static void main(String args[]) {
BankQueueThread bankQueueThread=new BankQueueThread();
AddUser adduser=bankQueueThread.new AddUser();
Thread th= new Thread(adduser);
th.start();
for(int i=0;i<3;i++){
OrderNormalQueue orn=bankQueueThread.new OrderNormalQueue();
th= new Thread(orn);
th.start();
}
OrderVIPQueue orv=bankQueueThread.new OrderVIPQueue();
th= new Thread(orv);
th.start();
}
//增加普通用户排队
public synchronized void addUser(int nums){
String s=""+(++num);
CustomerInfo customerInfo=new CustomerInfo();
customerInfo.setType("普通");
customerInfo.setId(s);
customerInfo.setFtime(nums);
customerInfo.setStime(datef.format(new Date()));
queue.add(customerInfo);
}
//增加VIP排队
public synchronized void addVipUser(int nums){
String s=""+(++num);
CustomerInfo customerInfo=new CustomerInfo();
customerInfo.setType("VIP");
customerInfo.setId(s);
customerInfo.setFtime(nums);
customerInfo.setStime(datef.format(new Date()));
queue.add(customerInfo);
vipqueue.add(s);
}
//普通窗口假设处理业务
public synchronized void removieUser(){
CustomerInfo cus=null;
if(queue.size()>0){
rnum++;
cus=(CustomerInfo)queue.remove(0);
if(vipqueue.size()>0&&vipqueue.get(0).equals(cus.getId())){
vipqueue.remove(0);
System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
}else{
System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
}
}
}
//VIP窗口假设处理业务
public synchronized void removieVipUser(){
String d="";
CustomerInfo cus=null;
if(vipqueue.size()>0){
rnum++;
d=vipqueue.remove(0);
for(int i=0;i<queue.size();i++){
cus=(CustomerInfo)queue.get(i);
if(cus.getId().equals(d)){
queue.remove(i);
System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
break;
}
}
}else if(queue.size()>0){
rnum++;
cus=(CustomerInfo)queue.remove(0);
System.out.println(Thread.currentThread().getName()+":>> "+rnum+" "+cus.getId()+" "+cus.getType()+" "+cus.getStime()+" "+cus.getFtime()+" <<办理业务完毕");
}
}
//用户排队
class AddUser implements Runnable{
Random rd=new Random();
int sd=0;
public void run() {
// TODO Auto-generated method stub
while(true){
sd=rd.nextInt(10);
if(sd==8){
addVipUser(sd);
}else{
addUser(sd);
}
if(queue.size()>=100){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//普通窗口业务受理
class OrderNormalQueue implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(true){
removieUser();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//VIP窗口业务受理
class OrderVIPQueue implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(true){
removieVipUser();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class CustomerInfo{
private String stime;
private String id;
private String type;
private int ftime;
public String getStime() {
return stime;
}
public void setStime(String stime) {
this.stime = stime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getFtime() {
return ftime;
}
public void setFtime(int ftime) {
this.ftime = ftime;
}
}
}