Summary of learning contents of teaching materials Tenth chapter: input and Output InputStream and OutputStream
- Stream is an abstraction of "input output", noting that "input and output" is relative to the program
- InputStream and OutputStream
- InputStream, OutStream provides streaming basic operations, and if you want to do processing for input/output data, you can use a wrapper class. The commonly used packager has the function of buffer bufferedoutputstream, Bufferedinputstream, with data conversion processing DataInputStream, DataOutputStream, ObjectInputStream, ObjectOutputStream, etc. with the ability to serialize objects.
- Read
- For reading of character data, Java SE provides the Java.io.Reader class, which abstracts the source of character data read-in. The Java.io.Writer class is provided for writing to character data. It abstracts the destination that the data writes out.
- Write
- FileReader, FileWriter can read and write to the document, read or write by default using the operating system default encoding to do character conversion. When you start the JVM, you can specify-dfile.encoding to specify the encoding used by the FileReader, FileWriter.
- binary files
- Standard input and output
- System.in: standard input, default association to keyboard (terminal input)
- System.out: Standard output, default associated to display (terminal output)
- System.err: standard error, default associated to display (terminal output)
- Input and output redirection: Setin,setout,seterr
- FileInputStream and FileOutputStream
- FileInputStream is a subclass of InputStream, you can specify a file name to create an instance, once the document is created, and then it can be used to read data. FileOutputStream is a subclass of OutputStream, you can specify a file name to create an instance, once the document is created, and then it can be used to write data. Use Close () to close the document when not in use, whether FileInputStream or FileOutputStream.
- Adorner class
- Decorator mode
- Bufferedinputstream and Bufferedoutputstream: internal cushioning for increased efficiency
- DataInputStream and DataOutputStream: Basic data type and byte conversion, API well, look at it.
- ObjectInputStream and ObjectOutputStream
- Java.io.Serializable interface
Character Processing classes
- Reader and writer
- Reader and writer also have some adorner classes to use. If the byte data that is processed by the stream actually represents the encoded data of some characters, and you want to convert the byte data to the corresponding encoded character, you can use InputStreamReader, outputstreamwriter to package the streaming data. BufferedReader, BufferedWriter can provide buffer function to reader and writer, and it will be helpful to the efficiency when processing character input/output. Printreader and PrintStream are very similar in use, but in addition to OutputStream packaging, PrintWriter can also package writer, providing print (), println (), Format () and other methods.
- Character processing adorners
To convert these byte data to the corresponding encoded characters, you can use InputStreamReader, OutputStreamWriter to package the stream data.
BufferedReader, BufferedWriter can provide buffer function to reader and writer, and it will be helpful to the efficiency when processing character input/output.
PrintWriter can also package writer, providing methods such as print (), println (), Format (), and so on.
11th chapter-Threads vs. Parallel API Threads
- Thread
- In Java, if you want to design a process independently of main (), you can compose a class operation Java.lang.Runnable interface, where the entry point of the process is the operation in the run () method.
In Java, the process that starts with main () is executed by the main thread, and you can create a thread instance to execute the run () method defined by the runnable instance.
- Thread and Runnable
- The JVM is a virtual machine that installs only one CPU, called the main thread, that executes the execution flow defined by main (). If you want to add a CPU to the JVM, that is to create a thread instance, to start the extra CPU is to invoke the start () method of the thread instance, and the additional CPU execution process entry point can be defined in the run () method of the Runnale interface.
- How to Compose multithreaded threads:
- Define the process in the run () method of the Runnable
- Inherit the thread class and redefine the run () method
- The advantage of manipulating the runnable interface is that it is more resilient, and your class has the opportunity to inherit other classes. If the thread is inherited, then the class is a thread, usually in order to directly take advantage of some of the methods defined in thread to inherit the thread.
- If you want to add a CPU to the JVM, that is to create a thread instance, to start the extra CPU is to invoke the start () method of the thread instance, and the additional CPU execution process entry point can be defined in the run () method of the Runnale interface.
- Thread life cycle
- Daemon Threads: The main thread starts execution from the main () method until the end of the main () method stops the JVM. If additional threads are started in the main thread, the default is to wait for all threads that are started to execute the run () method before aborting the JVM. If a thread is marked as a daemon thread, the JVM terminates automatically at the end of all non-daemon threads. Starting with the main () method is a non-daemon thread, and you can use the Setdaemon () method to set whether a thread is a daemon thread. You can also use the Isdaemon () method to determine whether a thread is a daemon thread.
- Some methods of threading control
- Interface definition Behavior
- Interrupt
- Sleep
- Join
- Stop (Deprecated)
- Resume (Deprecated)
- Suspend (Deprecated)
- Destroy (Deprecated)
- Thread stop, pause, restart to programmer to set implementation conditions or use the Concurrency API, do not call the deprecated method
- Sync: Synchronized and volatile
- Wait, notify, Notifyall
- Threadgroup
Each thread belongs to a line Cheng group, and if it is not specified, it is classified into the line Cheng group that produces the child thread, or you can specify the line Cheng group yourself, and the thread cannot be replaced once it has been grouped into a group. Some methods of Threadgroup can make a difference to all threads in a group. If you want to get all the threads in the group at once, you can use the enumerate () method. The Activecount () method gets the number of threads for the group.
Non-catching exceptions are setuncaughtexceptionhandler by thread instances () The set Thread.uncaughtexceptionhandler instance is processed after the thread Threadgroup, then the default Thread.uncaughtexceptionhandler.
Package cc.openhome;PublicClass tortoiseharerace2{Public static span class= "DT" >void main (String[ ] args) {Tortoise tortoise = new tortoise ( 10); Hare Hare = new hare (10); Thread tortoisethread = new thread (Tortoise); Thread harethread = new Thread (Hare); Tortoisethread.start ()}}
Parallel APIs
If you need a thread pool, read-write locks and other advanced operations. The Java.util.concurrent package is provided after JDK5 to build a more robust parallel application based on the API. The Java.util.concurrent.looks package provides lock, readwriter and condition interfaces as well as related operation classes, which can provide similar synchronized, wait (), notify (), The role of Notifyall () and more advanced features.
- Lock: The implicit lock object relative to the thread model, lock provides an explicit lock operation to simplify the application. Executors: Provides a set of HIGHLEVELAPI for performing and managing parallel tasks. Concurrentcollections (Parallel Collection): Contains a set of data structures that support parallel processing, greatly simplifying the difficulty of parallel programming. Atomicvariables (atomic variable): Reduces synchronization operations and avoids data inconsistencies. Fork/join Framework: Provides support for process operations.
- In contrast to the implicit lock object of the thread model, lock provides an explicit lock operation to simplify the application. Executors: Provides a set of HIGHLEVELAPI for performing and managing parallel tasks. Concurrentcollections (Parallel Collection): Contains a set of data structures that support parallel processing, greatly simplifying the difficulty of parallel programming. Atomicvariables (atomic variable): Reduces synchronization operations and avoids data inconsistencies. Fork/join Framework: Provides support for process operations.
- The Readwritelock:readwritelock interface defines the read lock and write lock behavior, and you can return the lock operand using the Readlock (), Writelock () method. Reentrantreadwritelock is the primary operation class for the Readwritelock interface, and the Readlock () method returns the Reentrantreadwritelock.readlock instance, Writelock () Breaking the law will return the Reentrantreadwritelock.writelock instance.
- The Condition:condition interface is used with lock, and the most basic usage is to achieve the function of the Wait (), notify (), Notifyall () method of object. The await (), signal (), Signalall () methods of condition can be considered as corresponding to the wait (), notify (), and Notifyall () methods of object.
- Executor: Provides a set of HIGHLEVELAPI for performing and managing parallel tasks.
- Scheduledthreadpoolexecutor:scheduledexecutorservice as the Executorservice sub-interface, as the name implies, allows you to work scheduling: Schedule () method is used to schedule runnable or callable instances to be executed once, and returns an instance of the future Subinterface scheduledfuture, for repetitive executions, using schedulewithfixeddelay () and Scheduleatfixedrate () method.
- Forkjoinpool: The so-called divide and conquer problem, refers to the solution of these problems, can be decomposed into sub-problems of the same nature, sub-problems can be decomposed into smaller sub-problems, the same nature of sub-problems to solve and collect the results of the operation, the whole problem is solved.
The Forkjoinpool differs from other executorservice operations in that it is chatting up the work-stealing calculus, and its established thread, if it completes the task at hand, will try to find and perform other tasks to establish the funding task, keep the thread busy, and effectively utilize the processor's capabilities. It is important to notethat the Forkjoin framework is suitable for computing dense tasks and is less suitable for situations where threading is easily blocked.
- Parallel collection
- Copyonwritearraylist operation of the list interface, the instance of this class in the write operation is to create a new array inside, and copy the reference of the original array index, and then write on the new array, after the completion of the original reference to the old array of variables reference to the new array.
- The Copyonwritearrayset operates the set interface, similar to the copyonwritearraylist.
- The Blockedqueue is the sub-interface of the queue, and the new put (), take () method is defined.
- Concurrentmap is a sub-interface of map that defines methods such as putifabsent (), remove (), and replace (). These methods are atomic operations.
- Concurrenthashmap is the Concurrentmap operation class, Concurrentnavigablemap is the sub-interface of CONCURRENTMAP, its operation class is Concurrentskiplistmap, Can be considered a treemap version that supports parallel operations.
Problems in teaching materials learning and the solving process
问题1:InputStreamand Reader differences OutputStream and Writer differences
The InputStream and OutputStream classes handle byte streams, and the smallest unit in the data stream is bytes (8 bit)
Reader and writer deal with the character stream, which involves the conversion of the characters encoding when processing the character stream.
Reader classes can convert characters in the input stream that use other encoding types to Unicode characters, and then allocate memory for them in memory
The writer class is able to convert in-memory Unicode characters to characters of other encoded types, and then write to the output stream.
- Question 2: Textbook 327 page tortoiseharerace2.java code running code run the first time to run the Turtle finished 10 steps, and then execute the Rabbit walk 10 steps, rather than according to the tortoise and the rabbit every round of the situation to carry out.
Analysis of the mistakes in exams last week
Although it was five points because most people did not submit, I was one of the three who submitted it, and I knew the wrong question.
1. What happens when you enter the "CD" command and press ENTER? A. Switching from the current directory to the root B. screen displays the current directory C. switch from the current directory to the top-level directory D. switch from the current directory to the user home directory (d)
3. Existing:
Import java.util.*;
Class Findstuff {
public static void Main (String[]args) {
Insert code here
C.put ("X", 123);
}
}
Insert to line 5th, which lines allow code compilation? (BDF)
A. Map c= new SortedMap ();
B. HashMap c= new HashMap ();
C. HashMap c= new Hashtable ();
D. SortedMap c= new TreeMap ();
E. ArrayList c= new ArrayList ();
F. Map C = new Linkedhashmap ();
4. Existing:
-list is a valid collection reference
-GetCollection () returns a reference to a legitimate collection
Which one is legal? (BE)
A. or (Object o; list)
B. For (Object o:getcollection ())
C. For (Object O:list.iterator ())
D. for (Lterator i; List.iterator (); I.hasnext ())
E. For (Lterator i=list.iterator (); I.hasnext (); )
5. Existing:
Import java.util.*;
Class Forintest {
Static List list=new ArrayList ();
}
public static void Main (String [] args) {
List. Add ("a"); list. Add ("B"); list. Add ("C");
Insert code here
System.out.print (o);
}
}
Which line is inserted into the 9th row causes the output "ABC"? D
A. for (Iterator o:list.iterator (); O.hasnext (); )
B. for (Iterator o:list)
C. for (Object O:list.iterator ())
D. for (Object o:list)
7. What is the statement that creates a generic arraylist that can only hold a string? B
A. Arraylist<int> al=new arraylist<int> ();
B. Arraylist<string> al=new arraylist<string> ()
C. ArrayList al=new arraylist<string> ();
D. Arraylist<string> al =new list<string> ();
12. What are the features of the set interface in the collection API? D
A. Duplicate elements are not allowed, elements are in order
B. Allow repeating elements, elements without order
C. Allow repeating elements, elements in order
D. Duplicate elements are not allowed, elements are not sequential
(D)
(C)
(AC)
(C)
(D)
(D)
(D)
Code Hosting rating 8
- Problem and solution process in textbook learning, one question plus 1 points
- Valid code for this week exceeds 300 branches (plus 2 points)
- -Beautifully formatted plus one point
- -Add 1 points to the progress bar to record the learning time and improvement situation
- -Learn to add 1 points to the wrong question
Learning progress Bar
20145234 Huangfei The sixth week of Java programming