标签:
线程安全:
如何自己手动创建一个线程
答:继承Thread类或实现Runnable接口
public class Mythread extends Thread{ @Override public void run() { for (int i = 1; i <=5; i++) { System.out.println("C"+i+"我是新的线程"); } } ---------------------------------------------------------------- package bao01; public class Mythread02 implements Runnable{ public void run() { Thread.currentThread().setName("武松接口"); for (int i = 1; i <=5; i++) { System.out.println("B"+i+"\t"+Thread.currentThread().getName()); } } }
依次实现几个方法
join():暂停某个线程
setDaemon()后台线程,又称守护线程,两个线程交替执行,当一个线程结束时,另一个线程也结束
Sleep():使线程休眠,单位是毫秒
------------------------------------------------------------------------------------------------------------------------
Java之序列化和反序列化的代码,其实也很简单,使用ObjectOutputStream将内存中的数据写入硬盘,再通过ObjectInputStream将硬盘的二进制数据读取到内存
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 class Demo01 { private static List<Person> list2; public static void main(String[] args) throws Exception { List<Person> list=new ArrayList<Person>(); Person per1=new Person("吴松",12); Person per2=new Person("白兴强",21); list.add(per1); list.add(per2); OutputStream os=new FileOutputStream("D://哈哈.txt"); ObjectOutputStream ops=new ObjectOutputStream(os); ops.writeObject(list); System.out.println("写入成功"); ops.close(); os.close(); list2 = fan("D://哈哈.txt"); for (Person person : list) { System.out.println(person.getName()); } } public static List<Person> fan(String path) throws Exception { InputStream is=new FileInputStream(path); ObjectInputStream ois=new ObjectInputStream(is); List<Person> list=(List<Person>)ois.readObject(); is.close(); ois.close(); return list; } }
标签:
原文地址:http://www.cnblogs.com/chimingyang/p/5766228.html