码迷,mamicode.com
首页 > 编程语言 > 详细

Java review-basic2

时间:2016-09-13 01:25:16      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

Java review-basic2

标签:

原文地址:http://www.cnblogs.com/whaochen205/p/5866952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!