Anonymous class OA local class without a name o Defined and instantiated in a single expression using the new operator. public void start () ActionListener listener new ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println("get the event "+e); } )//Need to give a;here,since it's an expression
Anonymous class lA local class without a name lDefined and instantiated in a single expression using the new operator. public void start (){ ActionListener listener = new ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println(“get the event ”+e); } }; //Need to give a ; here, since it’s an expression }
When to use anonymous class? oThe class has a short body o Only one instance of the class is needed oThe class is used right after it is defined oThe name of the class does not make your code any easier to understand
When to use anonymous class? lThe class has a short body lOnly one instance of the class is needed lThe class is used right after it is defined lThe name of the class does not make your code any easier to understand
Process o Programs often need to be doing multiple things at once OTalking on a network conn,process the data, listen to input,...etc OA process is a heavyweight OS-level unit of work OOS will assign piece of processor time to each processes,and take away the CPU when time is up OProcesses are protected from each other
Process lPrograms often need to be doing multiple things at once ¡Talking on a network conn, process the data, listen to input, …, etc lA process is a heavyweight OS-level unit of work ¡OS will assign piece of processor time to each processes, and take away the CPU when time is up ¡Processes are protected from each other
Threads o Threads are lightweight,application-level form of multi-tasking Also preemptive (when time is up,switch to another one) But not protected:all can see the same memory OWhich means much faster communication than classic "interprocess communication (IPC)
Threads lThreads are lightweight, application-level form of multi-tasking lAlso preemptive (when time is up, switch to another one) lBut not protected: all can see the same memory ¡Which means much faster communication than classic “interprocess communication (IPC)
All Java programs are MT OYou have the main()thread,and The garbage collector thread And any other thread by yourself
All Java programs are MT lYou have the main() thread, and lThe garbage collector thread lAnd any other thread by yourself