Thread Safety:
How to manually create a thread yourself
Answer: Inherit the thread class or implement the Runnable interface
Public classMythread extends thread{@Override Public voidrun () { for(inti =1; I <=5; i++) {System. out. println ("C"+i+"I'm a new thread"); } }----------------------------------------------------------------Package bao01; Public classMYTHREAD02 implements runnable{ Public voidrun () {Thread.CurrentThread (). SetName ("Wu Song Interface"); for(inti =1; I <=5; i++) {System. out. println ("B"+i+"\ t"+Thread.CurrentThread (). GetName ()); } }}
Implement several methods in turn
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
--------------------------------------------------------------------------------------------------------------- ---------
Java's serialization and deserialization code, in fact, is also very simple, using ObjectOutputStream to write in-memory data to the hard disk, and then through ObjectInputStream the hard disk's binary data read to the memory
Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import java.io.fileoutputstream;import Java.io.inputstream;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.io.outputstream;import Java.util.arraylist;import java.util.List; Public classDemo01 {Private StaticList<person>List2; Public Static voidMain (string[] args) throws Exception {List<Person> list=NewArraylist<person>(); Person Per1=NewPerson ("Woosung", A); Person Per2=NewPerson ("Baixingjiang", +); List.add (Per1); List.add (PER2); OutputStream OS=NewFileOutputStream ("d://haha. txt"); ObjectOutputStream Ops=Newobjectoutputstream (OS); Ops.writeobject (list); System. out. println ("Write Success"); Ops.close (); Os.close (); List2= Fan ("d://haha. txt"); for(person person:list) {System. out. println (Person.getname ()); }} Public StaticList<person>Fan (String path) throws exception{InputStream is=NewFileInputStream (path); ObjectInputStream Ois=NewObjectInputStream ( is); List<Person> list= (list<person>) Ois.readobject (); is. Close (); Ois.close (); returnlist; }}
Multi-thread one/serialization and deserialization of Java