5309 Java Programming 6th Week of study summary

Source: Internet
Author: User
Tags thread class

Summary of learning contents of teaching materials input and output inputstream and OutputStream
    • From an application perspective, the input stream can be used if the data is to be fetched from the source, and output streams can be used if the data is to be written to the destination. In Java, the input stream represents the object as an Java.io.InputStream instance, and the output stream represents the object as an Java.io.OutputStream instance. Regardless of the data source or destination, as long as you try to obtain an instance of InputStream or outputstream, then the input/output is done in a consistent manner without regard to the actual form of the source or destination.
    • All classes required for streaming I/O are included in the Java.io package. There are four basic classes in the Java.io package: InputStream, OutputStream, and reader, and writer classes that handle byte streams and character streams, respectively:
    • File File class
      Randomaccessfile Random Access File class
      InputStream BYTE input stream
      OutputStream BYTE output stream
      Reader Character input stream
      Writer Character output stream
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
Threads with parallel API threads
  • 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. 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.
  • 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.
    • 1, runnable is suitable for multiple same program code thread to handle the situation of unified resources, the virtual CPU (thread) and the Code of the program, data effectively separate, better reflect the idea of object-oriented programming
    • 2. Runnable can avoid limitations due to the single inheritance mechanism of java. Other classes can be inherited at the same time, but also to achieve the function of multithreading.
    • 3, runnable can increase the robustness of the program. Code can be shared by multiple threads.

      Thread life cycle
  • five states of the life cycle
    • new thread: When you create an instance (object) of the thread class, the thread enters a new state (not started). For example: Thread t1=new thread ();
    • Ready (runnable): The thread has been started and is waiting to be allocated to the CPU time slice, which means that the thread is waiting in the ready queue to get CPU resources. Example: T1.start ();
    • Run (running): The thread obtains CPU resources executing the task (the Run () method), at which point the thread will run until the end, unless the thread automatically discards the CPU resources or has a higher priority thread entry.
    • Death (dead): When a thread executes or is killed by another thread, the thread goes into a dead state, and the thread is no longer ready to wait for execution. Natural termination: Terminates after running the run () method normally. Abnormal termination: Call the Stop () method to let a thread terminate the run.
    • Blocked (blocked): For some reason, the running thread is getting out of the CPU and pausing its own execution, which goes into a blocked state. Sleeping: Use the sleep (Long T) method to put the thread into sleep mode. A sleeping thread can go into a ready state at the specified time. Waiting: Call the Wait () method. (Call the Motify () method back to the ready state). Blocked by another thread: Call the Suspend () method. (Call Resume () method restore)

      Thread basic state diagram
  • After invoking the thread instance start () method, the basic state is executable (Runnable), blocked (Blocked), in-Execution (Running). After instantiating the thread and executing the start () method, the thread enters the runnable state, at which point the thread has not really started executing the run () method and must wait for the queue to be queued to the CPU to execute, and the thread executes the run () method into the running state, and the thread looks like it is executing simultaneously , but in fact, at the same point in time, 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. The thread has priority, and the SetPriority () method of thread can be used to set priority. The minimum value is 1, the maximum value is 10, and the default is 5. 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. With multi-threading, when a thread enters the blocked, it is often one of the ways to improve performance by letting another line run into the CPU to avoid CPU idle.

    Parallel APIs
  • It is easy to implement basic parallel programming tasks based on the thread class and runnable interface programming, but it is difficult to implement complex parallel programs because of the detailed consideration of synchronization access to resources and the design of the necessary data structures to support synchronous access. From Java5, the Java platform provides a java.util.concurrent package and HIGHLEVELAPI simplifies the parallel programming model, and provides a lot of support for synchronizing access to data structures to meet programming needs.
  • Lock (Lock object): The lock object that is implicitly locked 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.
  • Copyonwritearraylist operates the list interface, as the name implies, 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 inside the original reference to the new array. For a scenario where a write operation is infrequent and frequent iterators are used, copyonwritearraylist can be used to improve the efficiency of the iterator operation. Blockingqueue 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 the Take () method, in the case of empty queue is blocked. Concurrentmap is a sub-interface of map that defines methods such as putifabsent (), remove (), and replace (). These methods are atomic operations. Putifabsent () The key/value object is not placed in Concurrentmap when the key object is not present, otherwise the value object corresponding to the key is returned. Remove () removes the key/value object only if the key object exists and the corresponding value object is equal to the specified value object. Replace () has two versions, one of which is only in the presence of the key object, and the corresponding value object equals the specified value object, the value object is substituted, and the other version is the value object is substituted when the key object exists.

    Experience

The weekly content is really too much, every week there is not something to accumulate under, to the sixth week I really can not stand, I began to decide assiduous, study hard to fill the previous knowledge.

5309 Java Programming 6th Week of study 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.