Some Java Knowledge Review

 

Set

HashMap

Reference: https://blog.csdn.net/jiary5201314/article/details/51439982

  1. HashMap is a Map based collection class that is used for storing Key & value pairs. 
  2. Structure: It has an array and linkedlist implemented in it. The array is used to store the hash of the key and the linked list is used to store the data and the key and other stuff.  
  3. HashMap stores the object in the form of Entry Class. And that Entry class is in the form of a linked list. 

Collisions in HashMap:

If two keys have the same corresponding hashcode, new value will be put into the tail of the linkedlist.

Hashmap resize:

放滿的情況。The data on the original array should be re-compute to get the new places for the new array. Of course, it will affect the performance.

HashTable

It is very like HashMap. Both of them store key/value pairs in a hash table.  The key is hashed, and the resulting hash code is used as the index.

HashMap和HashTable有什麼區別?

http://www.importnew.com/7010.html

1. HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.
2. HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.
3. HashMap is generally preferred over HashTable if thread synchronization is not needed

我們能否讓HashMap同步?

HashMap可以通過下面的語句進行同步:
Map m = Collections.synchronizeMap(hashMap);

HashSet

HashSet is based on hashmap. 

Hashset 如何保證集合的沒有重複元素?

It put the element as the key into the hashmap. When the two keys are the same, the hashmap will update the value rather than create a new one.

Hashmap 與hashset的區別?

Vector vs ArrayList 

  • Vectors are synchronized, ArrayLists are not.
  • Data Growth Methods

1.同一時刻只有一個線程能夠寫Vector。

2.當capacity if full, vector增長率爲目前數組長度的100%,而arraylist增長率爲目前數組長度
的50%.如過在集合中使用數據量比較大的數據,用vector有一定的優勢。

3. Vector由於使用了synchronized方法(線程安全)所以性能上比ArrayList要差

Multithreading

https://www.journaldev.com/1162/java-multithreading-concurrency-interview-questions-answers#process-vs-thread

What is the difference between Process and Thread? 進程線程區別

1.A process is a self contained execution environment and it can be seen as a program or application 

2.Thread is a single task of execution within the process. 

Benefits of multi-threaded programming? 多線程的好處

Multiple threads share the heap memory, so it’s good to create multiple threads to execute some task rather than creating multiple processes

Difference between user Thread and daemon Thread?一般線程和守護線程的區別

When we create a Thread in java program, it’s known as user thread. A daemon thread runs in background and doesn’t prevent JVM from terminating. When there are no user threads running, JVM shutdown the program and quits. A child thread created from daemon thread is also a daemon thread.

1.JVM 的垃圾回收線程是一個守護線程。

2.thread.setDaemon(true)

3.守護線程應該永遠不去訪問固有資源,如文件、數據庫,因爲它會在任何時候甚至在一 個操作的中間發生中斷。

How can we create a Thread in Java?

There are two ways to create Thread in Java – first by implementing Runnable interface and then creating a Thread object from it and second is to extend the Thread Class. Read this post to learn more about creating threads in java.

What are different states in lifecycle of Thread?

When we create a Thread in java program, its state is New. Then we start the thread that change it’s state to Runnable. Thread Scheduler is responsible to allocate CPU to threads in Runnable thread pool and change their state to Running. Other Thread states are Waiting, Blocked and Dead. Read this post to learn more about life cycle of thread.

Can we call run() method of a Thread class?

Yes, we can call run() method of a Thread class but then it will behave like a normal method. To actually execute it in a Thread, we need to start it using Thread.start() method.

What do you understand about Thread Priority?

We can specify the priority of thread but it doesn’t guarantee that higher priority thread will get executed before lower priority thread. Thread priority is an int whose value varies from 1 to 10 where 1 is the lowest priority thread and 10 is the highest priority thread.

What is context-switching in multi-threading?

Context Switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time.

How can we make sure main() is the last thread to finish in Java Program?

We can use Thread join() method to make sure all the threads created by the program is dead before finishing the main function.

How does thread communicate with each other?

When threads share resources, communication between Threads is important to coordinate their efforts. Object class wait(), notify() and notifyAll() methods allows threads to communicate about the lock status of a resource.

  • yield()方法是停止當前線程,讓同等優先權的線程或更高優先級的線程有執行的機會。 如果沒有的話,那麼 yield()方法將不會起作用,並且由可執行狀態後馬上又被執行。
  • join 方法是用於在某一個線程的執行過程中調用另一個線程執行,等到被調用的線程執行結束後,再繼續執行當前線程。如:t.join();//主要用於等待 t 線程運行結束,若無此句, main 則會執行完畢,導致結果不可預測。
  • notify 方法只喚醒一個等待(對象的)線程並使該線程開始執行。所以如果有多個線程等待一個對象,這個方法只會喚醒其中一個線程,選擇哪個線程取決於操作系統對多線程管 理的實現。
  • notifyAll 會喚醒所有等待(對象的)線程,儘管哪一個線程將會第一個處理取決於操作系統的實現

What is volatile keyword in Java

private static volatile int MY_INT = 0;

All the threads read it’s value directly from the memory and don’t cache it. This makes sure that the value read is the same as in the memory.

What is ThreadLocal?

We know that all threads of an Object share it’s variables

Every thread has it’s own ThreadLocal variable and they can use it’s get() and set() methods to get the default value or change it’s value local to Thread. 

What is Java Thread Dump, How can we get Java Thread dump of a Program?

分析thread的工具

Thread dump is list of all the threads active in the JVM, thread dumps are very helpful in analyzing bottlenecks in the application and analyzing deadlock situations. There are many ways using which we can generate Thread dump – Using Profiler, Kill -3 command, jstack tool etc. I prefer jstack tool to generate thread dump of a program because it’s easy to use and comes with JDK installation. 

What is Deadlock? How to analyze and avoid deadlock situation?

Deadlock is a programming situation where two or more threads are blocked forever。

To analyze a deadlock, we need to look at the java thread dump of the application. we need to look out for the threads with state as BLOCKED and then the resources it’s waiting to lock, every resource has a unique ID using which we can find which thread is already holding the lock on the object.

Avoid:

  1. 加鎖順序(線程按照一定的順序加鎖)lock the thread in order.
  2. 加鎖時限(線程嘗試獲取鎖的時候加上一定的時限,超過時限則放棄對該鎖的請求,並釋放自己佔有的鎖)
  3. 死鎖檢測。如果檢測到,可以釋放所有的鎖並且回退狀態

設計模式 design pattern

 

JVM篇

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章