Java面试2

1, java创建线程有几种不同的方式?你喜欢哪一种?为什么?

1、 继承Thread类;
2、实现Runnable接口;
3、应用程序可以使用Executor框架来创建线程

 实现Runnable接口这种方式更受欢迎,因为这不需要继承Thread类。在应用设计中已经继承了别的对象的情况下,这需要多继承(而Java不支持多继承),只能实现接口

 

 

2, HashMap和Hashtable有什么区别?

1.Hashtable是Dictionary的子类,HashMap是Map接口的一个实现类;
2.Hashtable中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。即是说,在多线程应用程序中,不用专门的操作就安全地可以使用Hashtable了;而对于HashMap,则需要额外的同步机制。但HashMap的同步问题可通过Collections的一个静态方法得到解决:
Map Collections.synchronizedMap(Map m)
这个方法返回一个同步的Map,这个Map封装了底层的HashMap的所有方法,使得底层的HashMap即使是在多线程的环境中也是安全的。
3.在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断

 

3, 编程实现:线程A向队列Q中不停写入数据,线程B从队列Q中不停读取数据(只要Q中有数据)。

//接口中有两个 一个是向队列中写push方法  一个是从队列中读
public interface StackInterface {
    public void push(int n);

    public int[] pop();
}

// 上边接口的实现类。
public class SafeStack implements StackInterface {
    private int top = 0;
    private int[] values = new int[10];
    private boolean dataAvailable = false;

    public void push(int n) {
        synchronized (this) {
            while (dataAvailable) {
                try {
                    wait();
                } catch (InterruptedException e) {

                }
            }
            values[top] = n;
            System.out.println("压入数字" + n + "步骤1完成");
            top++;
            dataAvailable = true;
            notifyAll();
            System.out.println("压入数字完成");
        }
    }

    public int[] pop() {
        synchronized (this) {
            while (!dataAvailable) {
                try {
                    wait();
                } catch (InterruptedException e) {

                }
            }
            System.out.println("弹出");
            top--;
            int[] test = {values[top],top};
            dataAvailable = false;
            //唤醒正在等待压入数据的线程
            notifyAll();
            return test;
        }
    }
}
//读线程
public class PopThread implements Runnable{
    private StackInterface s;
    public PopThread(StackInterface s){
        this.s = s;
    }
    public void run(){
        while(true){
            System.out.println("-->"+s.pop()[0]+"<--");
            try{Thread.sleep(100);
            }catch(InterruptedException e){
                
            }
        }
    }
}
//写线程
public class PushThread implements Runnable{
    private StackInterface s;
    public PushThread(StackInterface s){
        this.s = s;
    }
    public void run(){
        int i = 0;
        while(true){
            java.util.Random r = new java.util.Random();
            i = r.nextInt(10);
            s.push(i);
            try{
                Thread.sleep(100);
            }catch(InterruptedException e){}
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值