Some Java Knowledge Review

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sengo_GWU/article/details/88361777

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. 死锁检测。如果检测到,可以释放所有的锁并且回退状态

synchronized

synchronized方法与代码块区别

 

同步方法锁的范围比较大,而同步代码块范围要小点,一般同步的范围越大,性能就越差

Volatile

同步变量读写操作。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. 访问volatile变量时不会进行加锁操作。

设计模式 design pattern

JVM篇

猜你喜欢

转载自blog.csdn.net/Sengo_GWU/article/details/88361777