First, serialization and deserialization
The process of converting an object to a sequence of bytes is called serialization of an object .
The process of reverting a sequence of bytes to an object is called deserialization of the object .
There are two main uses for serialization of objects:
1) The object's byte sequence is permanently saved to the hard disk, usually stored in a file;
2) A sequence of bytes that transmits an object over the network.
The serialization key code is as follows:
Defines the person class for serialization. Override the ToString method, define no parameters, and construct with parameters
Defining classes
Public classMyserialize { Public Static voidMain (string[] args)throwsIOException {outputstream os=NewFileOutputStream ("Save.bin"); ObjectOutputStream Oos=Newobjectoutputstream (OS); List<Person> list=NewArraylist<person>(); Person P1=NewPerson ("Zs", "BJ"); Person P2=NewPerson ("hh", "Ah"); Person P3=NewPerson ("Xixi", "HF",); List.add (p1); List.add (p2); List.add (p3); Oos.writeobject (list); System.out.println ("Serialization succeeds!!!" "); }}
The deserialization key code is as follows:
Public class public staticvoidthrows Exception { InputStreamis = New FileInputStream ("Save.bin"); ObjectInputStream ois=new ObjectInputStream (IS); List<Person> list= (list<person>) ois.readobject (); for (person person:list) { System.out.println (person); }}}
Operating effect:
Second, multi-threaded implementation of two ways
① Inherit thread
② implementation Runnable
1. Methods of inheriting the thread class although I am listed as a multithreaded implementation, thread is essentially an instance of the Runnable interface, which represents an instance of a thread, and the only way to start a thread is through the start () instance method of the thread class. The start () method is a native method that starts a new thread and executes the run () method. This way of implementing multithreading is simple, by extend thread directly through your own class, and by copying the run () method, you can start a new thread and execute your own defined run () method. For example:
Package Cn.b.happy; Public class extends thread{@Overridepublicvoid run () { System.out.println ("I am a new thread! ");}}
MyThread t1=New MyThread (); System.out.println (Thread.CurrentThread (). GetName ()); T1.start ();
2, if your own class has extends another class, you can not directly extends Thread, at this time, must implement a runnable interface, as follows:
package Cn.b.happy; public class Implthread implements runnable{@Override
public void run () {Thread.CurrentThread (). SetName ( child thread 2" ); System.out.println ( "Child thread" ); System.out.println ( I am a child thread "
Implthread t2=New implthread (); In order to start implthread, you need to instantiate a thread first and pass in your T2 instance: thread tt=new thread (T2); Tt.run (); System.out.println (Thread.CurrentThread (). GetName ()); }
Three
Join (): Pauses a thread
Setdaemon () Background thread, also known as daemon thread, two threads alternately execute, when one thread ends, another thread ends
Sleep (): Causes the thread to hibernate, in milliseconds
Key code:
PackageCn.c.happy; Public classSleepthreadextendsthread{@Override Public voidrun () {Thread.CurrentThread (). SetName ("Child Threads"); for(inti = 1; I <=5; i++) { /*try {//Hibernate thread.sleep (3000); } catch (Interruptedexception e) {e.printstacktrace (); }*/System.out.println ("B" +i+ "\ T" +Thread.CurrentThread (). GetName ()); } }}
PackageCn.c.happy; Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception {sleepthread S1=NewSleepthread (); S1.setdaemon (true); S1.start (); //Alternating execution for(inti = 1; I <=5; i++) { if(i==3) {s1.join ();//call the Join method after execution completes other methods} System.out.println ("A" +i+ "\ T" +Thread.CurrentThread (). GetName ()); }}}
How threads are implemented and serialized deserialized. Java