20145307 Chenjunda Sixth Week Java Learning summary

Source: Internet
Author: User

20145307 Chenjunda Sixth Week Java Learning Summary Knowledge points carding Tenth chapter S1

• To take the data out of the source, you can use the input stream, or you can use the output stream to write the data to the destination. In Java, the input stream represents an instance of Java.in.InputStream, and the output stream represents an instance of the object as Java.io.Outputstream.

• You can design a general dump () method that accepts InputStream and OutputStream instances, which represent the source of the data being read, and the destination of the output, in the case where neither source nor destination is known.

• Each time the data is read from InputStream, the byte data is placed first, and her read () method attempts to read the Btye data and returns the bytes read in.

• To read a document into and save it as another data, the command line can be manipulated by the following Java cc.openhome.Copy C:\workspace\Main.java C:\workspace\Main.txt

• You can specify the InputStream instance using the system's Setin () method to reassign 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. How to operate the main operation of 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.

· DataInputStream and DataOutputStream primarily provide methods for reading and writing Java basic data types, which are automatically converted between the specified type and bytes.

S2

· Reader, writer also has some adorner class to use, if the stream processing of 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

• Parse several common subclasses: StringReader can package strings as read sources, StringWriter can be written to the destination, and finally ToString () takes out all the characters that are written to the string. Similar to CharArrayReader, Chararraywriter, the char array is treated as a read source and written to the destination.

• If streaming 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 ().

11th Chapter S1

• Single-threaded program: Starts the procedure from main () program entry point start to end only one process

• Multi-threaded Program: There are multiple processes

• In Java, processes starting from main () are executed by the main thread

Thread: If you want to add the main thread, create a thread instance, and to start the additional main thread is to invoke the start () method of the thread instance

• Additional threads execute the entry point of the process in two ways: you can define the inherited thread class in the Runnable run () method, and redefine the run () method

Daemon Threads:

• The main thread executes 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 automatically terminates at the end of all non-Daemon threads

• Starting with the main () method is a non-daemin thread that can use the Setdaemon () method to set whether a thread is a Daemon thread

• Use the Isdaemon () method to determine whether a thread is a Daemon thread

Thread Basic state diagram

• After invoking the Thread instance start () method, the basic state is executable (Runnable), blocked (Blocked), in Execution (Running)

• Threads appear to be executing at the same time, but in fact at the same point, a CPU can only execute one thread, but the CPU will constantly switch threads and the switch moves quickly, so it looks like it is executing simultaneously

setpriority (): 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, more than 1 to 10 out of the set value will be thrown illegalargumentexception

• The higher the number, the higher the precedence, the more priority the typesetting is placed on the CPU, and if the priority is the same, the flow execution

• How to Improve performance: Use multi-threading, when a thread enters Blocked, let another line into the CPU execution, to avoid the CPU idle

interrupt (): a thread that enters the Blocked state can be called by another thread, the interrupt () method of the thread, leaving it out of the Blocked state

• Inserting threads

Join (): 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

• Stopping threads

• After the thread finishes the run () method, it enters Dead, and the thread entering the Dead cannot call the start () method again, or it will throw a illegalthreadstateexception

• If you want to stop a thread, it's best to do it yourself, let the thread run out of the process, rather than calling the thread's Stop () method

Stop (): Directly calls the thread's stop () method, ignoring the set release, get lock process, the thread will directly release all locked objects, which may cause the object to fall into an unexpected state

Threadgroup

• Each thread belongs to a line Cheng group

• When each thread is generated, it is grouped into a line Cheng group, which is generated in that group, and if not specified, it is assigned to the line Cheng group that produces the child thread, or to the line Cheng group that the thread is assigned to, and it cannot be replaced once it is grouped into a group.

setmaxpriority (): Sets the maximum priority for all threads in a group

Enumerate (): Get all the threads in the group at once:

thread[] Threads = new Thread[threadgroup1.activecount ()]; Threadgroup1.enumerate (threads);

Activecount (): Gets the number of threads in the group, the enumerate () method passes to the thread array, which sets the threading object to each array index

uncaughtexception (): The JVM calls this method for processing when an exception occurs in a thread in a group that is not snapped. If Threadgroup has a parent threadgroup, the Uncaughtexception () method of the parent threadgroup is called, or if the exception is a Threaddeath instance, and if nothing is done, then the exception is called. Printstracktrace (), if you must define the thread exception handling behavior in Threadgroup, you can redefine this method

uncaughtexception () method The first parameter can take the thread instance where the exception occurred, and the second parameter would have the exception object

• After JDK5, if the thread in the Threadgroup has an exception, the Uncaughtexception () method processing order is:

• If Threadgroup has a parent threadgroup, the Uncaughtexception () method of the parent Threadgroup is called

• Otherwise, see if Thread uses the Setuncaughtexceptionhandler () method to set the Thread.uncaught-exceptionhandler instance, and then call its uncaughtexception ( ) method

• Otherwise, see if the exception is a Threaddeath instance, if "yes" then do nothing and if "no" call the exception of Printfstracttrace ()

synchronized

• 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 the method, the execution method must obtain the lock on the instance

• The line Chengjo enters the Blocked due to an attempt to execute a synchronized chunk, and after the lock is taken, it returns to the Runnable state, waiting for the CPU typesetting to enter the Running state

Java synchronized provides the ability to re-enter the synchronization, that is, when the thread obtains an object lock, if the execution of the process to execute the synchronized, the object to try to obtain the lock is the same source, you can directly execute

volatile

synchronized required to achieve the mutex and visibility of the marked block, the mutex refers to the synchronized block at the same time only one thread, visibility refers to the thread left the synchronized block, the other line of contact is the first thread changed the state of the object

• Volatile can be declared on a variable, which means that the variable is unstable and volatile, which is likely to be accessed under multiple threads, which guarantees the visibility of the variable, that is, if the variable value is changed by a thread, the other thread must see the change. Variables marked as volatile, do not allow thread cache, variable value access must be in shared memory

volatile guarantees that the visibility of a single variable, thread access to the variable must be in shared memory, not in their own memory space cache variables, threads to share in-memory variables, the other thread must be seen

• Wait and notice

Wait (), notify (), Notifyall () are methods defined by the object, which can be used to control whether the thread releases the lock on the object or to notify the thread to participate in the lock contention.

Wait (): To invoke the Wait () method of a locked object during the execution of a synchronized-scoped program code, the thread frees the object lock and enters the object waiting for the collection to be blocked, other threads can compete for the object lock, and the thread that obtains the lock can execute Synchronized the scope of the program code. Wait () can specify the waiting time until after which the thread will join the schedule again, and if the specified time is 0 or unspecified, the thread will continue to wait, only to be interrupted or to be notified that it can participate in the scheduling

noyify (): When a competition-locked object calls Noyify (), a thread is randomly notified from the object waiting collection to join the schedule, and the notified thread will compete with other threads to lock the object before executing the synchronized again.

Notifyall (): If Notifyall () is called, all threads in the wait collection are notified of participating schedules, which compete with other threads for object locking

Lock, Readwritelock and Condition

One of the main operating classes of the lock interface is reentrantlock, which can achieve synchronized function, and also provide additional functions

• To lock the lock object, you can call its lock method, only the thread that has the lock object locked can continue to execute the program code, to contact the lock, you can call the lock object's unlock ()

The lock interface also defines the Trylock () method, which returns true if a thread calls Trylock () to obtain a lock, and returns False if the lock cannot be obtained and no blocking occurs.

Readwritelock

The Readwritelock interface defines the read lock and write lock behavior, and you can use the Readlock (), Writelock () method to return the lock action object

Reentrantreadwritelock.readlock operation of the lock interface, when calling its lock () method, if no reentrantreadwritelock.writelock call the Lock () method, That is, when there is no write lock, a read lock can be obtained

Reentrantreadwritelock.writelock operation of the lock interface, when calling its lock () method, if there is no reentrantreadwritelock.readlock or Reentrantreadwritelock.writelock calls the Lock () method, which means that a write lock can be obtained without any read or write lock

Validate (): Verify that the stamp was not acquired by another exclusive lock, and if so, returns False if the stamp is 0 and returns false condition

The condition interface is used with Lock, and the most basic usage is to achieve the function of the Wait (), notify (), Notifyall () method of Object.

signal (): To notify a thread in the Wait collection, you can call the signal () method

Signalall (): If you want to notify all waiting threads in the collection, you can call Signalall ()

• A Condition object can represent a waiting set, can repeatedly call Lock's newcondition (), get multiple condition instances, which means that there can be more than one waiting set

Executor

• Starting with JDK5, the Java.util.concurrent.Executor interface is defined to separate the Runnable designation from the actual execution

Scheduledexecutorservice class Scheduledthreadpoolexecutor is a subclass of threadpoolexecutor with thread pool and scheduling functions

• Parallel Collection

Copyonwritearrayset operation of the List interface, when an instance of this class is written, a new array is created internally, the reference to the original array index is copied, and a write operation is made on the new array, and after the write is completed, the variables referencing the old array of the internal reference to the new array

Bllockingqueu is the sub-interface of the queue, the new definition of put () and take (), and so on, the line Chengjo call put () method, the queue is full, the case will be blocked, the line Chengjo call took () method, in case the queue is empty will be blocked

Experience

The content of the week is really too much, every week there is not something to accumulate under, to the sixth week I really can not stand, I decided to start this week every night 617 watching video from the beginning to learn, or so snowball, to the end of nothing learned, wasted time. That's ironic.

20145307 Chenjunda Sixth Week Java Learning summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.