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

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篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过使用Java SOME/IP库来实现SOME/IP通讯。SOME/IP(Scalable service-Oriented MiddlewarE over IP)是一种用于在汽车电子系统中进行通信的协议。下面是一些实现SOME/IP通讯的步骤: 1. 导入Java SOME/IP库:首先,需要将Java SOME/IP库添加到项目的依赖中。可以从官方网站或其他可靠的资源获取该库。 2. 创建SOME/IP客户端和服务器:使用Java SOME/IP库提供的API,可以创建SOME/IP客户端和服务器。客户端用于发送请求,服务器用于接收和处理请求。 3. 定义SOME/IP服务接口:定义SOME/IP服务接口,包括服务的名称、方法和参数。这些接口定义可以使用IDL(Interface Definition Language)或其他类似的工具来完成。 4. 实现SOME/IP服务:根据定义的SOME/IP服务接口,实现相应的服务逻辑。这包括处理请求、执行相应的操作,并返回结果。 5. 配置网络连接:配置网络连接以确保SOME/IP客户端和服务器可以相互通信。这可能涉及到设置IP地址、端口号等网络参数。 6. 启动SOME/IP服务器和客户端:在应用程序中启动SOME/IP服务器和客户端,以便它们可以开始通信。 7. 进行通信:使用SOME/IP客户端发送请求,并使用SOME/IP服务器接收和处理请求。可以根据需要进行多次通信。 8. 处理响应:在SOME/IP客户端中处理来自SOME/IP服务器的响应。根据服务接口定义,解析响应并采取相应的操作。 这些是实现Java中SOME/IP通讯的基本步骤。具体的实现细节和代码可能会因库的不同而有所差异。建议参考Java SOME/IP库的文档和示例代码以获取更详细的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值