The advance of Java -- IO, Thread(Day05)


1. IO

 

①Serialize: When we need to transfer a oject from a computer to another computer's disk, we use serialize. ObjectOutputStream oos

Dog d = new Dog("H",12,"sssssssssss");
		try (
				ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream("d:/a.properties"));
				) {
			
			s.writeObject(d);
			s.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

②Deserialize: ObjectInputStream ois

try (
				ObjectInputStream i = new ObjectInputStream(new FileInputStream("d:/a.properties"));
				)
		{	
			Dog d = (Dog) i.readObject();
			System.out.println(d);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

③Transient: the member variables modified by transient will not participate in the serialize

transient String address;

④writeObject() & readObject()

⑤RandomAccess: program can read and write file freely.

try {
			RandomAccessFile f = new RandomAccessFile("d:/a.properties", "rw");//Read and Write
			f.seek(1);//Insert into '1' position
			f.write("hello ".getBytes());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

2. NIO:

①The difference between IO and NIO: The input and ouput method of traditional IO is blocking. Conversely, NIO will map file or a part of area of file into memory so that it can access file as fast as memory.

②Channel & Buffer:

(1)All the data in NIO need to transferred by Channel, it provide a map method. Traditional input - output is cater to Stream, but NIO is face to block.

(2)All the data need to preserve in Buffer, including the data in Channel.

CharBuffer c = CharBuffer.allocate(8);
		System.out.println("Position " + c.position());the next index can be read.
		System.out.println("limit " + c.limit());// the data behind limit cannot read
		System.out.println("Capacity " + c.capacity());
		c.put("a");
		c.put("b");
		c.put("c");
		System.out.println("Position " + c.position());
		c.flip();//changing the position of 'limit' to 'position', position set '0'
		System.out.println("Position " + c.position());
		System.out.println("limit " + c.limit());
		System.out.println(c.get(1));
		System.out.println("Position " + c.position());
		c.clear();
		System.out.println(c.get(2));
		System.out.println("Position " + c.position());
		System.out.println("limit " + c.limit());
		System.out.println("Capacity " + c.capacity());
	}


3. Thread:

①Process == Application

②Thread is execution unit exist in process. It can only access to resources in this process. When system create a process, it will also create a main thread.

③Thread & Runnable:

Since 'Runnable' is a interface, so we can realize other supclasses and  interfaces easily. But we still need a type of Thread to receive.

public class MyRunnable implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName());
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	

	}

}
public static void main(String[] args) {
		Runnable r = new MyRunnable();
		Thread t = new Thread(r);
		t.start();
	}

④The state of Thread:

⑤Thread.currentThread: gaining current Thread.

for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().isDaemon());
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		
		Thread t = new Thread(){
			public void run() {		
			System.out.println(Thread.currentThread().getState());
			
		}
		};
		t.start();

⑥Daemon: it will forwordlly stop should declare before current thread start.




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值