Difference: The permanent generation was canceled in jdk1.8 and replaced by Metaspace. This space does not occupy the memory of the jvm virtual machine, but the memory of the physical machine; jdk8 adds lambda expressions and local access. Variables, functional interfaces and other features.
##Comparison between jdk1.7 and jdk1.8
1. jdk1 .7 Syntax
1) You can use binary to represent integers, starting with 0b. 2) Switch statement supports string type3) The try block can be closed manually without finally, it can be closed directly in the try block4) Catch multiple exception description: The Catch exception type is final so it cannot be modified. This feature will generate less bytecode and reduce code redundancy. Keep the exception type when rethrowing the exception. 5) You can use underscores in numbers, which is more friendly. But pay attention to the rules for adding underlines. Underlines can only be between numbers. Underlines must not be used at the beginning or end of numbers. 6) When creating a generic instance, the generic type of the instance will be automatically inferred based on the generic type when the variable is declared. 7) Pass non-reified parameters in variable parameter methods to improve compilation warnings and errors. 8) More informative backtrace.2. Some new features of NIO2
1) Implement asynchronous non-blocking IO and Proactor2) Recursively search the file tree and file extension Search3. Jdbc
1) You can use try-with-resources to automatically close Connection, ResultSet, and Statement resource objects. 2) RowSet1.1 (interface model): Introducing the RowSetFactory interface and RowSetProvider class, you can create various row sets supported by the JDBC driver. The rowset implementation here actually converts some operations on the SQL statement into methods The operation encapsulates some functions. 3) The JDBC-ODBC driver will be deleted in jdk8.4. Concurrency tool enhancement
1) fork-join, the Fork/Join framework is a framework for parallel execution of tasks provided by Java7. It is a The large task is divided into several small tasks, and finally the results of each small task are summarized to obtain the framework of the results of the large task. (Make full use of multi-core features to decompose large problems into sub-problems. Multiple CPUs can solve multiple sub-problems at the same time, and finally merge the results). 2) The ThreadLocalRandon concurrent random number generation class ensures the thread safety of concurrent random number generation. In fact, threadlocal is used. 3) The phaser classjava5 has a concurrent package. There are semaphore (semaphore), CountDownLatch (lock), and CyclicBarrier (fence). The phaser class optimizes CountDownLatch and CyclicBarrier.Semaphore: A semaphore can declare multiple locks (including one lock: this is a mutually exclusive semaphore).
For example: If a room can only accommodate 5 people, the extra people must wait outside the door. How to do it? One solution is: hang five keys outside the room, and each person who enters takes one key away. Those without keys cannot enter the room but wait outside. Every time someone comes out, put the key back to its original place to make it easier for others to enter again. is mainly used to control the maximum number of threads that can be accessed. The default is unfair. The fairness of a lock can be explicitly declared through the constructor.Latching:
Meaning: CountDownLatch can be understood as a counter setting the initial value during initialization. When a thread needs to wait for some operations first When completed, the await() method needs to be called. This method puts the thread to sleep until all waiting threads have completed execution. Each time the countDown() method is called, the internal counter is decremented by 1 until it wakes up when the counter reaches 0. This can be understood as a special CyclicBarrier. The thread synchronization point is special, starting when the internal counter value reaches 0. Method: Two core methods: countDown() and await(). countDown(): Decrement the internal counter maintained by CountDownLatch by 1, and call it when each waiting thread completes. await(): When the thread executes CountDownLatch, the thread will be put to sleep Example: Example of a meeting: The meeting can only start in the conference room when all the participants have arrived.Fence:
Meaning: A fence allows two or more threads to synchronize at a certain rendezvous point. When a thread reaches the rendezvous point, it calls the await() method to wait for other threads. After the thread calls the await() method, CyclicBarrier will block the thread and put it into sleep state to wait for the arrival of other threads. When the last thread calls the await() method, CyclicBarrier will wake up all waiting threads and then these threads will continue execution. CyclicBarrier can pass in another Runnable object as an initialization parameter. When all threads arrive at the rendezvous point, the CyclicBarrier class executes the Runnable object as a thread.Method: await(): Put the thread to sleep until the arrival of the last thread and wake up all sleeping threads.
Differences from CountDownLatch:
Accepts a Runnable type object as subsequent execution after all threads arrive at the rendezvous point
Does not explicitly call the CountDown() method
CountDownLatch can generally only be used once, CyclicBarrier can be used multiple times
Application scenario: Multiple threads do tasks, and after reaching the rendezvous point for synchronization, they are handed over to subsequent threads for summary.
Phaser:
Meaning: A more complex and powerful synchronization auxiliary class. It allows concurrent execution of multi-stage tasks. When we have concurrent tasks and need to break them into several steps for execution (CyclicBarrier is divided into two steps), we can choose to use Phaser. The Phaser class mechanism synchronizes threads at the end of each step. When all threads have completed this step, the next step is allowed to be executed. Like other synchronization tools, the number of tasks participating in synchronization operations in the Phaser class must be initialized. The difference is that the number of tasks can be dynamically increased or decreased.
Function: arriveAndAwaitAdvance(): Similar to the await() method of CyclicBarrier, it waits for other threads to arrive and continues execution synchronously. arriveAndDeregister(): Log out the thread executed here from Phaser. isTerminated(): Determine whether Phaser is terminated. register(): Register a new participant into Phaser. This new participant will be regarded as a thread that has not completed this phase. forceTermination(): Force Phaser to enter the termination state
Example: Use the Phaser class to synchronize three concurrent tasks. These three tasks will look for files with a .log extension that have been modified in the past 24 hours in three different folders and their subfolders. This task is divided into the following three steps: find files, filter files, and print results. And after searching for files and filtering files, analyze the results. If they are empty, log off this thread from Phaser. In other words, in the next stage, the thread will not participate in running. In the run() method, the phaser's arriveAndAwaitAdvance() method is called at the beginning to ensure that all threads are started before starting to search for files. After both the find file and filter file phases, the results are processed. That is: if the result is empty, then remove the thread. If it is not empty, then wait for all threads in this stage to complete the step before executing the next step uniformly. Finally, after the task is executed, all threads in Phaser are logged out.
Phaser actually has two states: active state and terminated state. Phaser is active when there are threads participating in synchronization. And synchronized at the end of each stage. When all threads participating in synchronization are unregistered, Phase is in the terminated state. In this state, Phaser has no task participants.
The main function of Phaser is to execute multi-stage tasks and ensure thread synchronization at each stage point. Participants can also be conditional or removed at each stage point. It mainly involves the methods arriveAndAwaitAdvance() and register() and arriveAndDeregister().
4) Networking enhancement
New URLClassLoader close method can close resources in time, and subsequent reloading of class files will not cause resource occupation or inability to release problems.
5) MultithreadedCustom Class Loaders
Solve the deadlock problem that may be caused by concurrent loading of classes. This is solved by some new versions of jdk1.6, and jdk7 has also made some optimizations.
Java1.8
1. The default method of the interface
Before Java1.8, all methods in the interface were required to be abstract methods. Java 8 Allows us to add a non-abstract method implementation to the interface, just use the default keyword.
2. Lambda expression
It will allow us to pass behavior into the function. Before Java 8, if you wanted to pass behavior into a function, your only option was an anonymous class, which required 6 lines of code. The most important line of code that defines the behavior is buried in the middle and doesn't stand out enough. Lambda expressions replace anonymous classes, eliminating templates and allowing code to be written in a functional style. This sometimes results in better readability and clearer expression.
3. Functional interface
If an interface defines a unique abstract method, then this interface becomes a functional interface. A very valuable property of functional interfaces is that they can be instantiated using lambdas.
4. Method and constructor references
Use keywords to pass method or constructor references.
5. Lambda Scope
The way to access the outer scope in a lambda expression is very similar to that in the old version of anonymous objects. You can directly access outer local variables marked final, or instance fields and static variables.
6. Access local variables
You can directly access outer local variables in lambda expressions.
7. Accessing object fields and static variables
The difference from local variables is that the fields and static variables of the instance are both readable and writable inside the lambda. This behavior is consistent with anonymous objects.
8. Default method of access interface
JDK1.8 API contains many built-in functional interfaces, such as Comparator or Runnable interfaces commonly used in old Java. These interfaces have added annotations so that they can be used with lambdas.
Java 8API also provides many new functional interfaces to make work more convenient. Some interfaces are from the Google Guava library. Even if you are familiar with these, it is still necessary to take a look at these. How to extend it to lambda.
If you want to read more related articles, please visit PHP Chinese website! !
The above is the detailed content of What is the difference between jdk1.7 and jdk1.8. For more information, please follow other related articles on the PHP Chinese website!