Java review-basic2

1.Implement a thread-safe (blocking) queue:

Class Producer implements Runable{
    Private final BlockingQueue queue;
    Producer (BlockingQueue q){queue=q;}
    Public void run(){
        try{
                   while(true){queue.put(produce());
            }catch(InterruptedException ex){…handle…}
           }
    Object produce(){…..}
}

Class consumer implements runnable{
    Private final BlockingQueue queue;
    Consumer(BlockingQueue q){queue=q;}
    Public void run(){
       Try{
       While(true){consume(queue.take());}
    }catch(InterruptedExpection ex){… handle …}
    }
    void consumer(Object x){…}
}
    

2. Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a BlockingQueue happen-before actions subsequent to the access or removal of that element from the BlockingQueue in another thread.

3. What are some ways to implement a singleton in Java

(1)
Public class SingletonObject
{
       private Singleton(){ }

    public static SingletonObject getSingletonObject(){
          return ref;
    }
}

private static SingletonObject ref;

(2)
Public class singletonObject{
    Private singleton(){}
    Public static singletonObject getSingletonObject(){
    Return ref;
    }

}

4.Checked vs unchecked exceptions, finalize

1) Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword. like IOexpection

2) Unchecked are the exceptions that are not checked at compiled time instead of runtime. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions. In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

3) Finalize: Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Also, the garbage collector is not guaranteed to run at any specific time. In general, what I‘m trying to say is finalize() is probably not the best method to use in general unless there‘s something specific you need it for.

5. What is a queue, stack, heap

Queue: FIFO

Stack: FILO, system allocate space on memory

Heap: by using new keyword allocate

6. What happens in the system during a recursive call

Defining recursion is easy – any routine that calls itself is a recursive routine. A call stack is a data structure used by the program to store information about the active subroutines in a program. The main reason for having a call stack is so that the program can keep track of where a subroutine should return control to once it finishes executing. A stack frame is a part of the call stack, and a new stack frame is created every time a subroutine is called. The stack frame is used to store all of the variables for one invocation of a routine.

时间: 2024-08-07 08:40:15

Java review-basic2的相关文章

java review几处小问题集锦

1 线程池使用不当 我们的调度系统需要将一堆会员分配给相应的人员来处理,流程如以下伪代码所示: public void dispatch() { while (true) { List<Member> memberList = getUnassignedMemberList(); //获取所有未分配的会员 for(Member each : memberList) { singleDispatch(each); //为每一个会员分配相应的人员处理 } try { Thread.sleep(10

java:Review(Oracle-HTML-CSS)

20170708_review: 1.oracle: 对表的操作: 使用命令行建立一张表:create table 表名 (列名 列名的类型 primarty key, ....); alter table 表名 add constraints pk_表名_列名 primary key(需要建立的主键列名); 删除一张表:drop table 表名; truncate(清空表中的数据,但是表不被删除) 修改一张表:alter table 表名(....); 查询一张表:desc 表名; 对数据的

Java Review for....

1.标识符      用于命名程序的对象,如方法名,变量名,规则是: a.大小写敏感 b.由英文字符,文字字符,美元符号,下划线和数字组成,但不能以数字开头 c.不能是关键字 2.%:求余运算符    23%-4 结果为3 3.++i:先引用变量i,后再执行i+1赋值给i作为运算结果 i++:先执行i加1,然后再将结果赋值给i作为运算结果进行引用 4.数组的命名三种 a.int  intArray[ ]; intArray=new   int[5]; b.int    intArray[ ]=n

一些常见JAVA问题

原文:https://blog.csdn.net/weiyongxuan/article/details/45920765 一.Java的异常的基类是java.lang.Throwable 二.守护线程 1.java中只有用户线程和守护线程 2.守护线程的典型是GC,垃圾回收器 3.守护线程是用来服务用户线程的 三. volatile关键字volatile关键字用在多线程同步中,可保证读取的可见性,JVM只是保证从主内存加载到线程工作内存的值是最新的读取值,而非cache中.但多个线程对 vol

Java时间格式字符串与Date的相互转化

目录 将Date转化为格式化字符串 时间格式字符串转化为Date @ 将Date转化为格式化字符串 将Date转化为格式化字符串是利用SimpleDateFormat类继承自 java.text.DateFormat类的format方法实现的: public final String format(Date date):将日期格式化成日期/时间字符串. //获取当前时间 Date date = new Date(); //定义转化为字符串的日期格式 SimpleDateFormat sdf =

【dataStructure】 Arrays and Java Source Review

According to the order of data structure book, Arrays should be introduced in the frist time. When reviewing the some information related to arrays, I feel shocked that many useful classes and methods in java language has been ignored. Now set a simp

mysql 保存emoji时报,数据库报错:Caused by: java.sql.SQLException: Incorrect string value: &#39;\xF0\x9F\x98\x82\xF0\x9F...&#39; for column &#39;review&#39; at row 1

错误原因:我们可以看到错误提示中的字符0xF0 0x9F 0x98 0x84 ,这对应UTF-8编码格式中的4字节编码(UTF-8编码规范).正常的汉字一般不会超过3个字节,为什么为出现4个字节呢?实际上是它对应的是智能手机输入法中的表情.那为什么会报错呢?因为mysql中的utf-8并不是真正意义上的utf-8,它只能存储1~3个字节长度的utf-8编码,如果想存储4个字节的必须用utf8mb4类型.不而要使用utf8mb4类型,首先要保证Mysql版本要不低于 MySQL 5.5.3. 常用

Code Review:C#与JAVA的哈希表内部机制的一些区别

看C#与JAVA源码时发现C#与JAVA哈希表的实现略有不同,特此分享一下. 我觉得看哈希表的机制可以从"碰撞"这里划线分为两部分来分析. 1,发生碰撞前 在发生碰撞前决定get与put的速度唯一因素是通过哈希函数计算键值位置的速度.而占用空间大小取决于需要的桶的数量(取决于极限装载值(load factor)(假设已知需要放入哈希表中元素的数量)),和桶的大小. C#的默认装载系数=0.72 // Based on perf work, .72 is the optimal load

java面试题大全

java面试笔试题大汇总     第一,谈谈final, finally, finalize的区别. 最常被问到. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统). 第四,&和&&的区别. 这个问得很少. 第五,HashMap和Hashtable的区

java代码分析及分析工具

java代码分析及分析工具 一个项目从搭建开始,开发的初期往往思路比较清晰,代码也比较清晰.随着时间的推移,业务越来越复杂.代码也就面临着耦合,冗余,甚至杂乱,到最后谁都不敢碰. 作为一个互联网电子商务网站的业务支撑系统,业务复杂不言而喻.从09年开始一直沿用到现在,中间代码经过了多少人的手,留下了多少的坑,已经记不清楚了,谁也说不清了. 代码的维护成本越来越高.代码已经急需做调整和改善.最近项目组专门设立了一个小组,利用业余时间做代码分析的工作,目标对核心代码进行分析并进行设计重构. 代码分析