NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Serialization
- Serialization là chuyển từ một object tồn tại thành một mảng byte,
thường dùng để lưu trữ Object(value của các properties trong Object) và khôi phục nó trên heap kể cả khi shutdown chương trình.
- Mảng byte này thường sẽ đc lưu trữ vào file, nơi mà không bị ảnh hưởng bởi chương trình đang chạy hay bị shutdown.
- Serialization không thể lưu trữ biến static do static được sử dụng bởi nhiều instance
- Sử dụng keyword transient để bỏ qua các thuộc tính(properties) khi Serialization, khi deserialized các thuộc tính này sẽ mang giá trị mặc định (int = 0, float = 0.0, boolean = false, object = null)
- Khi class extend từ 1 class không Serialization, thì khi deserialized, class con sẽ đc khôi phục các value đó, nhưng class cha sẽ như 1 object mới thay vì giống như khi Serialization
- Nếu 2 instance cùng có properties là object và reference tới 1 object trong heap. Khi deserialized, sẽ chỉ khôi phục 1 object và reference 2 properties tới object đó trong heap.
- Nếu deserialized không tìm thấy class Serialization, JVM sẽ ném Exception và dừng việc deserialized.
- Khi deserialized, JVM không sử dụng hàm khởi tạo(constructer), nói cách khác là nó không được chạy.
- Trong cây kế thừa, nếu có 1 class không serializable, thì các class phía trên đều không được khôi phục như lúc Serialization mà sẽ sử dụng hàm khởi tạo(constructer) để tạo 1 object mới.
- Các thay đổi sau sẽ ảnh hưởng đến quá trình deserialized:
+ Xóa hoặc thay đổi type(double -> boolean) hoặc từ biến non-static thành static cho variable
+ Thay đổi non-transient thành transient
+ Thay đổi cây kế thừa
+ Hủy bỏ implements Serializable của class
- Các thay đổi không ảnh hưởng đến quá trình deserialized:
+ Thêm các variable, khi đó các var sẽ mang giá trị mặc định
+ Thêm/xóa các lớp khỏi cây kế thừa
+ Thay đổi access modifier cho variable
+ Thay đổi transient thành non-transient. Lúc này các variable không được lưu trữ trước đó sẽ mang giá trị mặc định.
- JVM luôn kiểm tra serialVersionUID đầu tiên, của object được deserialized và class deserialized, nếu khác thì sẽ có ngoại lệ đc ném ra
- Để Serialization một đối tượng, ta cần một ObjectOutputStream -> FileOutputStream -> File. Deserialized sẽ làm ngược lại.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Giao diện Callable tương tự Runnable, trong đó cả hai đều được thiết kế cho các lớp có phiên bản có khả năng được thực thi bởi một luồng khác.
Callable sử dụng method call(), Runable sử dụng method run().
Tuy nhiên, Runnable là void nên không trả về kết quả và không thể ném ngoại lệ được kiểm tra. Còn Callable sẽ trả về Future<T> (bất kỳ) hoặc throw Exception.
-Triển khai đa luồng:
+ Tạo class implement Interface Runnable
+ Override method run()
+ Trong main: Tạo instance của class implement (T1). Tạo instance của class Threat(Runnable target) -> new Threat(T1)

- Có thể sử dụng các Thread pool để quản lý thay vì tạo thủ công.
+ ExecutorService từ Java 1.5, luôn sử dung Callable và Runnable để quản lý và chạy:
-> Khởi tạo:
Executors.newSingleThreadExecutor(): trong pool chỉ có duy nhất 1 thread
Executors.newCachedThreadPool(): Số lượng thread tạo ra sẽ do hệ thống quyết định, khi 1 thread hoàn thành sẽ chờ 60s trước khi bị hủy hoặc sẽ đc tái sử dụng cho 1 nhiệm vụ khác
Executors.newFixedThreadPool(n): Số lượng thread tạo ra do ng dùng quyết định. Nếu có task nhưng đã max số lượng thì sẽ đc đưa vào hàng chờ.

executorService.shutdown(); để dừng việc thêm task, các task đang chạy hoặc trong hàng chờ vẫn đc thực thi đến khi hoàn thành. Là hàm void nên không return. Nên đc sử dụng để đóng tài nguyên ExecutorService.
executorService.shutdownNow(); để dừng việc thêm task, hủy bỏ các task trong hàng chờ. Các task đang chạy không được đảm bảo dừng ngay tại thời điểm này. Return List<Runnable> trả về danh sách task đã đc thêm vào hàng đợi nhưng chưa đc thực hiện.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.