2013金山校园招聘Java笔试题

时间:2013-03-06 编辑:Ying 手机版

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

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

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

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

  第三题 线程的p、v操作

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

  package common;

  import org.junit.Test;

  /**

  * PV操作示例

  * @author xcbeyond

  *

  * 2012-10-2下午08:05:09

  */

  public class PVOperator {

  public static void main(String [] args){

  Store s = new Store(5);

  Produce pro1 = new Produce(s);

  Produce pro2 = new Produce(s);

  Consumer con1 = new Consumer(s);

  Consumer con2 = new Consumer(s);

  pro1.start();

  con1.start();

  pro2.start();

  con2.start();

  }

  }

  /**

  * 仓库类:临界资源

  *

  */

  class Store{

  private final int maxSize; //最大容量

  private int count;

  public Store(int size){

  maxSize = size;

  count = 0;

  }

  /**

  * 添加资源

  */

  public synchronized void add(){

  while(count >=maxSize){

  System.out.println("----仓库满了!----");

  try {

  wait();

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  count++;

  System.out.println(Thread.currentThread().toString()+ "put" +count);

  notifyAll();

  }

  public synchronized void remove() {

  while(count <= 0) {

  System.out.println("----仓库空了!----");

  try {

  wait();

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  System.out.println(Thread.currentThread().toString()+ "get"+count);

  count--;

  notify();

  }

  }

  /**

  * 生产者:P操作

  */

  class Produce extends Thread {

  private Store s;

  public Produce(Store s) {

  this.s = s;

  }

  @Override

  public void run() {

  while(true){

  s.add();

  try {

  Thread.sleep(1000);//只是为了利于查看线程间的同步,所以延迟1s

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  }

  }

  /**

  * 消费者:V操作

  */

  class Consumer extends Thread {

  private Store s;

  public Consumer(Store s) {

  this.s = s;

  }

  @Override

  public void run() {

  while(true) {

  s.remove();

  try {

  Thread.sleep(1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  }

  }

2013金山校园招聘Java笔试题相关推荐