2013金山校园招聘Java笔试题

原文转载:http://blog.csdn.net/xcbeyond/article/details/7993570

第一题 :栈内存与堆内存的特点与区别,java中是怎样分配的?

                  栈内存中用来存放基本数据类型(8种基本类型)和对象的引用变量,存取速度比堆快,栈中的数据可以被共享使用,堆内存中用来存放new创建的对象和数组对象。

第二题:对象序列化,作用,那些不能序列化?

               对象序列化是为了能够让对象像其他变量数据一样能够长久的保存下来,其实质是把对象在内存中的数据按照一定的规则,变成一系列的字节数据,然后写入到流中。没有实现java.io.Seralizabled接口的类不能实例化。关于序列化更加详细的介绍:Java序列化的那些事

第三题 线程的p、v操作

              线程对于程序员而言,是比较重要的一块知识,不会线程编程,就算不上一个合格的程序员。因此,线程也是各个公司笔试面试必考的内容之一。PV操作本是操作系统中相关的内容,简单来说,P操作是申请资源,V操作是释放资源。本题最好可以用生产者/消费者来实现PV操作最为合适,同时也考虑到了多线程同步的问题。举例说明:

[java]  view plain copy print ?
  1. package common;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. /** 
  6.  * PV操作示例 
  7.  * @author xcbeyond 
  8.  * 
  9.  * 2012-10-2下午08:05:09 
  10.  */  
  11. public class PVOperator {  
  12.     public static void main(String [] args){  
  13.         Store s = new Store(5);  
  14.         Produce pro1 = new Produce(s);  
  15.         Produce pro2 = new Produce(s);  
  16.         Consumer con1 = new Consumer(s);  
  17.         Consumer con2 = new Consumer(s);  
  18.         pro1.start();  
  19.         con1.start();  
  20.         pro2.start();  
  21.         con2.start();  
  22.     }  
  23. }  
  24. /** 
  25.  * 仓库类:临界资源 
  26.  * 
  27.  */  
  28. class Store{  
  29.     private  final int maxSize; //最大容量  
  30.     private int count;    
  31.       
  32.     public Store(int size){  
  33.         maxSize = size;  
  34.         count = 0;  
  35.     }  
  36.     /** 
  37.      * 添加资源 
  38.      */  
  39.     public synchronized void add(){  
  40.         while(count >=maxSize){  
  41.             System.out.println("----仓库满了!----");  
  42.             try {  
  43.                 wait();  
  44.             } catch (InterruptedException e) {  
  45.                 e.printStackTrace();  
  46.             }  
  47.         }  
  48.         count++;  
  49.         System.out.println(Thread.currentThread().toString()+ "put" +count);  
  50.         notifyAll();  
  51.     }  
  52.       
  53.     public synchronized void remove() {  
  54.         while(count <= 0) {  
  55.             System.out.println("----仓库空了!----");  
  56.             try {  
  57.                 wait();  
  58.             } catch (InterruptedException e) {  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.         System.out.println(Thread.currentThread().toString()+ "get"+count);  
  63.         count--;  
  64.         notify();  
  65.     }  
  66. }  
  67. /** 
  68.  * 生产者:P操作 
  69.  */  
  70. class Produce extends Thread {  
  71.     private Store s;  
  72.       
  73.     public Produce(Store s) {  
  74.         this.s = s;  
  75.     }  
  76.     @Override  
  77.     public void run() {  
  78.         while(true){  
  79.             s.add();  
  80.             try {  
  81.                 Thread.sleep(1000);//只是为了利于查看线程间的同步,所以延迟1s  
  82.             } catch (InterruptedException e) {  
  83.                 e.printStackTrace();  
  84.             }  
  85.         }  
  86.     }  
  87. }  
  88. /** 
  89.  * 消费者:V操作 
  90.  */  
  91. class Consumer extends Thread {  
  92.     private Store s;  
  93.       
  94.     public Consumer(Store s) {  
  95.         this.s = s;  
  96.     }  
  97.       
  98.     @Override  
  99.     public void run() {  
  100.         while(true) {  
  101.             s.remove();  
  102.             try {  
  103.                 Thread.sleep(1000);  
  104.             } catch (InterruptedException e) {  
  105.                 e.printStackTrace();  
  106.             }  
  107.         }  
  108.     }  
  109. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值