面试笔试题目

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

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

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

               对象序列化是为了可以让对象像其余变量数据同样可以长久的保存下来,其实质是把对象在内存中的数据按照必定的规则,变成一系列的字节数据,而后写入到流中。没有实现java.io.Seralizabled接口的类不能实例化。关于序列化更加详细的介绍:http://blog.csdn.net/xcbeyond/article/details/7993588编程

第三题 线程的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();
			}
		}
	}
}
相关文章
相关标签/搜索