20145216 "Java Program Design" 6th Week study summary textbook Study content summary tenth chapter input/output 10.1 InputStream and OutputStream
If you want to take the data out of the source, you can use the input stream, and if you write the data to the destination, you can use the output stream. In Java, the input stream represents the object as an Java.in.InputStream instance, and the output stream represents the object as an Java.io.Outputstream instance.
- In the case where neither source nor destination is known, a generic dump () method can be designed that accepts InputStream and OutputStream instances, representing the source of the data being read, and the destination of the output.
import java.io.*;p ublic class Io {public static void dump (InputStream src,outputstream dest) Throws IOException {try (inputstream input = src; OutputStream output = dest) {byte[] data = new byte[1024]; int length; while (length = input.read (data))! =-1) {output.write (data, 0, length); } } }}
Each time the data is read from InputStream, the byte data is placed first, and her read () method tries to read the Btye data and returns the bytes that are in the reading.
To read a document in and save it as another data, you can do this by command line:java cc.openhome.Copy c:\workspace\Main.java C:\workspace\Main.txt
You can use the Setin () method of the system to specify the InputStream instance and re-specify the standard input source.
FileInputStream is a subclass of InputStream, the main operation of InputStream's read () abstract method, FileOutputStream is a subclass of OutputStream, the main operation of its write () method.
Bytearrayinputstream is a subclass of InputStream, you can specify the byte data to create an instance, the main operation its read () abstract method; Bytearrayoutputstream is a subclass of OutputStream. The operation method that primarily operates its write ().
The stream decorator itself does not change the behavior of InputStream and OutputStream, but after getting the data, do some processing.
- bufferedinputstream and Bufferedoutputstream provide buffer functionality primarily internally.
import java.io.*;p ublic class Bufferedio {public static void dump (InputStream src, outputstream dest) Throws IOException {try (inputstream input = new Bufferedinputstream (SRC); OutputStream output = new Bufferedoutputstream (dest)) {byte[] data = new byte[1024]; int length; while (length = input.read (data))! =-1) {output.write (data, 0, length); } } }}
DataInputStream and DataOutputStream primarily provide methods for reading and writing Java basic data types, which are automatically converted between the specified type and bytes. Example code see GIT open source China.
10.2 Character Processing class
- writer also has some adorner classes to use, if the stream processing of the byte data, actually represents some characters of encoded data, and you want to convert these byte data to the corresponding encoded characters, you can use InputStreamReader and OutputStreamWriter The sample code is as follows:
import java.io.*;p ublic class Charutil {public static void dump (Reader src, Writer dest) throws IOException { Try (Reader input = src; Writer output = dest) {char[] data = new char[1024]; int length; while (length = input.read (data))! =-1) {output.write (data, 0, length); } } }}
To use Charutil.dump () to read a document into a string and display it in text mode, you can do the following:
import java.io.*;public class CharUtilDemo { public static void main(String[] args) throws IOException { FileReader reader = new FileReader(args[0]); StringWriter writer = new StringWriter(); CharUtil.dump(reader, writer); System.out.println(writer.toString()); }}
Parsing several commonly used subclasses: StringReader can package strings as read sources, StringWriter can be written to the destination, and finally the Tong ToString () gets all the characters that are written into the string. Similar to CharArrayReader, Chararraywriter, the char array is treated as a read source and written to the destination.
If the stream byte data is processed, the byte data is converted to the corresponding encoding system, which can be packaged using Inputstringreader, Inputstringwriter.
BufferedReader, BufferedWriter can provide buffer for reader, writer,
PrintWriter and PrintStream processing can be packaged for OutputStream, PrintWriter can also package writer, providing methods such as print (), println (), Format ().
Chapter 11th threading and Parallel API11.1 threading Introduction
Single-threaded programs: The program starts from the main () program entry point from the beginning to the end of only one process.
multithreaded programs: A program has multiple processes.
How to Compose multithreaded threads:
- Operate the Runnable interface and define additional processes in run ();
- Inherit the thread class and define additional processes in run ();
- Difference: The advantage of operating the Runnable interface is that it is more resilient and the class has the opportunity to inherit other classes; If you inherit the thread class, that class is a thread, usually to take advantage of some of the methods defined in thread to inherit the thread.
If additional threads are started in the main program, 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 automatically terminates at the end of all non-daemon threads.
After invoking the thread instance start () method, the basic state is executable (Runnable), blocked (Blocked), in-Execution (Running).
Thread has its priority, can use the thread's SetPriority () method to set the priority, can be set to a value of 1 to 10, the default is 5, the setting value beyond 1 to 10 will throw illegalargumentexception. The higher the number the higher the priority, the more priority the shift is to the CPU, and if the priority is the same, the output is executed.
If a thread is running and a B thread is allowed to join in the process, and the B thread executes before continuing the A-thread process, you can use the join () method to complete the requirement.
After the thread finishes the run () method, it enters dead, and the thread that enters dead (or has already called the Start () method) cannot call the start () method again, or it throws a illegalargumentexception.
If you want to stop a thread, it's a good idea to do it yourself, and let the thread run out of its due process rather than calling the thread's Stop () method. Not only does the stop thread have to operate on its own terms, the thread pauses, restarts, but also depends on the requirements, rather than directly calling suspend (), resume (), and so on.
When each thread is generated, it is grouped into a line Cheng group, which is generated in which group, such as a thread in main () main process, which belongs to the main line Cheng group. If not specified, the line Cheng group that produces the child thread is included, and the line Cheng group can be assigned by itself, and the group cannot be replaced once the thread has been grouped into a group.
Each object will have an internal lock, or a monitoring lock. Blocks marked as synchronized will be monitored, and any thread executing the synchronized block must first obtain the specified object lock.
If synchronized is marked on a method, the execution method must obtain the lock on that instance. Synchronized can be used not only as a method, but also as a way of describing a sentence.
Java synchronized provides the ability to re-enter the synchronization, that is, when the thread obtains an object lock, if the execution of the synchronized, the object that attempts to acquire the lock is the same, you can directly execute.
During program code that executes the synchronized scope, if the wait () method of the locked object is called, the thread frees the object lock and enters the object waiting for the collection to be blocked, and other threads can compete for the object lock. The thread that gets the lock can execute the synchronized scope of the program code.
The thread that waits for the collection does not participate in CPU scheduling, wait () can specify the wait time, and then the thread will join the schedule again, and if the time is 0 or unspecified, the thread will wait until it is interrupted (call interrupt () or tell notify ()) to participate in the scheduling.
When a competition-locked object calls notify (), a thread is randomly notified from the object waiting set to join the schedule, and before the synchronized is executed again, the notified thread competes with the other thread for the object lock, and if Notifyall () is called, All threads in the waiting collection are notified of participating schedules, which compete with other threads for object locking.
11.2 Parallel APIs
When using the Advanced parallel API, the Operation object of the lock interface implements synchronized functionality.
When using the Advanced parallel API, the condition interface's operands can implement the Wait (), notify (), Notifyall () functions of object.
When using the Advanced parallel API, the Operation object of the future interface allows you to achieve the results in the next step.
Problems in teaching materials learning and solving process problems:
The tenth chapter of the various input and output of the flow and the difference between the class is unclear.
Resolution process:
By reading the textbook and summarizing the knowledge point, we get the following analysis:
InputStream:字节输入流,抽象化字节数据读入的来源OutputStream:字节输出流,抽象化字节数据写出的目的地InputStream子类:FileInputStream、ByteArrayInputStream、DataInputStream、ObjectInputStreamOutputStream子类:FileOutputStream、ByteArrayOutputStream、DataOutputStream、ObjectOutputStream、PrintStreamReader:字符输入流,抽象化字符数据读入的来源Writer:字符输出流,抽象化字符数据写出的目的地Reader子类:FileReader、BufferedReader、StringReader、CharArrayReader、InputStreamReaderWriter子类:FileWriter、BufferedWriter、StringWriter、CharArrayWriterr、OutputStreamWriter、PrintWriter
Problems in code debugging and resolving process issues:
Do not understand the role and meaning of start in the p327 page code snippet (below) in the book.
Thread tortoisethread = new Thread (tortoise);
Thread harethread = new Thread (hare);
tortoiseThread.start(); hareThread.start();
Resolution process:
By reading the key content, get the following explanations:
从main()开始的流程会由主线程执行,可以创建Thread实例来执行Runnable实例定义的run()方法,要启动线程执行指定流程,必须调用Thread实例的start()方法。
Last run success, results
This week's code hosting
Other (sentiment, thinking, etc., optional)
In the process of studying Chapter 11 or 11, I felt that the knowledge points of the two chapters are very abstract and difficult to understand, and the code examples in the book are closely related to the previous content, because the knowledge points in the previous chapters are not thorough enough to understand the code. There is always a need to revisit the textbook to review the previous content to get a little bit of code.
Learning progress Bar
|
lines of code (new | /Cumulative)
Blog volume ( | new/cumulative)
Learning time (new/cumulative) |
Important growth |
Goal |
4500 rows |
30 Articles |
350 hours |
Be able to use Java freely |
First week |
150/150 |
2/2 |
15/15 |
Learn basic Java-related knowledge |
Second week |
200/350 |
1/3 |
20/35 |
Learn the basic syntax of Java |
Third week |
450/800 |
1/4 |
25/60 |
Learn about object and package knowledge |
Week Four |
687/1487 |
1/5 |
30/90 |
Learn about inheritance and interface knowledge |
Week Five |
803/2290 |
1/6 |
30/120 |
Learn about exception handling and collection and map knowledge |
Week Six |
910/3200 |
2/8 |
40/160 |
Learn about input, output, and threading |
Resources
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
- ..
20145216 Shi Yao "java Program Design" 6th Week study Summary