java面试代码题目 java经典面试编程题( 二 )


public synchronized void set(String name,String content){
if(!flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
this.setName(name) ; // 设置名称
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
this.setContent(content) ; // 设置内容
flag= false ; // 改变标志位,表示可以取走
super.notify() ;
}
public synchronized void get(){
if(flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
System.out.println(this.getName() +
" -- " + this.getContent()) ;
flag= true ; // 改变标志位,表示可以生产
super.notify() ;
}
public void setName(String name){
this.name = name ;
}
public void setContent(String content){
this.content = content ;
}
public String getName(){
return this.name ;
}
public String getContent(){
return this.content ;
}
};
class Producer implements Runnable{ // 通过Runnable实现多线程
private Info info = null ;// 保存Info引用
public Producer(Info info){
this.info = info ;
}
public void run(){
boolean flag = false ; // 定义标记位
for(int i=0;i12;i++){
if(flag){
this.info.set("生产者","压入子弹") ; // 设置名称
flag = false ;
}else{
this.info.set("消费者","射出子弹") ; // 设置名称
flag = true ;
}
}
}
};
class Consumer implements Runnable{
private Info info = null ;
public Consumer(Info info){
this.info = info ;
}
public void run(){
for(int i=0;i24;i++){
this.info.get() ;
}
}
};
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info = new Info(); // 实例化Info对象
Producer pro = new Producer(info) ; // 生产者
Consumer con = new Consumer(info) ; // 消费者
new Thread(pro).start() ;
new Thread(con).start() ;
}
};
java面试题1 。JVM配置能承受多少就多少
2java面试代码题目,
StringTokenizer token=new StringTokenizer(src,",") ;
StringBuffer ok=new StringBuffer();
StringBuffer no=new StringBuffer();
while(token.hasMoreTokens()){
String temp=nextToken();
try{
Float.parseFloat(temp);
}catch(Exception e){
on.append(temp).append.(",");
}
ok.append(temp).append.(",");
}
on就是不能转换java面试代码题目的数据;ok就是能转换的数据
3 。
import java.io.*;
import java.util.*;
class PathFileCount{
private MapString,Integer mapType=new HashMapString,Integer();
public PathFileCount(){
}
public void count(String path){
try{
File file=new File(path);
if(!file.exists()) return ;
if(file.isFile()) {
//System.out.println(file.getName());
String[] name=file.getName().split("\\.");
String lastName=name[name.length-1];
if(mapType.containsKey(lastName)) mapType.put(lastName,mapType.get(lastName)+1);
else mapType.put(lastName,1);
}//if(file.isFile())
else{
File[] list=file.listFiles();
for(int i=0;ilist.length;i++){
//System.out.println(list[i].getPath());
count(list[i].getPath());
}//for
}//else
}catch(Exception e){
}//catch
}//method
public void print(){
for(String lastName:mapType.keySet()){
System.out.println(lastName+""+mapType.get(lastName));
}
}//method
public void sort(int tag){

推荐阅读