advance programming期末复习
主要是课件上需要记忆的,还有空手写代码时的小细节。考前串一下。
week1 thread
thread 的概念:Thread is a lightweight process which has its own cache, can run and share data with other threads.
2 ways to creat thread.
1).Extend thread. e.g.Thread t=new thread(); trigger default contributor with no argument to run.
2).implement Runnable interface, and creat new thread.
start()和run()的区别:
When start() is invoked, the thread goes into ready status, which means it is eligble for running. Excution of the thread starts when run() method is called by JVM.
3 way terminate thread: 不写了 课件上有
Synchronized 和volatile的区别:
only primitive variable can be declared as volatile; volatile cannot protect complex codes; Access to volatile variable cannot has the potential to be protected.
Entry set&Wait set
When a thread wants to access to a block of code protected by synchronized, but it doesn't have the lock, it is in entry set.
When it get the lock, but other threads are excuting that code, the thread needs to enter wait set by callng the wait() method. And this thread will stay in wait() until notify().
lock&read write lock:
lock means only one thread can be permitted to excute this block of code, but sometime it is slow. Read write lock permits more readers at the same time and only one writer.
Synchronized 3种作用范围:
1.synchronized method 2.括号里加some objects 3. an object can use itself as a lock e.g.synchronized(this)
week 1 socket
概念:creat connection to remote computers to receive and send data.
空手写代码:server端和client端
【注】都要import java.net.*; import java.io.*;
都要bufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),true))); //可以说是巨难背了
server端:有一个变量 public static final int PORT=8080;
有两个try
client端:InetAddress addr=InetAddress.getByName(127.0.0.1);
如果要multiple clients 就写个while loop 里 s.accept();
week 2
Serializable
import java.io.*; serializable is marker interface, no method to implement.
OutputStream outputFile=new FileOutputStream("....ser");
ObjectOutputStream cout=new ObjectOutputStream(outputFile);
outputFile.writeObject(object);
最后加两个close().
RMI
概念:send java objects and invoke methods form remote computer.
主要4部分: interface,impl, server, client
interface: import java.rmi.remote; 因为它要extends Remote.
impl: import java.rmi.*; import java.rmi.server.*; 然后extends UnicastRemoteObject implement interface.
server和client都import java.rmi.naming;
Stub和Skeleton的作用
Stub 3点
1)Implement same interface as server; 2)serialize argument to call the remote method and send to server; 3)receive and deserialize the result
Skeleton 4点
1)Receive the remote call of method 2)deserialize the parameters; 3)find a suitable method on behalf of remote client 4)serilaize and send back the result to client
RMI registry: a program that keeps mapping of remote object interfaces.
HTTP
HTTP request response header会画
TCP 结构图
...
week 3,week 4还没整理完就考试了, 就酱吧~