Close the frame window event in 1.AWT:
Addwindowlistener (New Windowadapter () {//Shutdown event handling
public void windowclosing (WindowEvent e) {
SetVisible (FALSE);
System.exit (0); Program shutdown
}
});
2. Streaming provides sequential access to data, which is especially useful for network applications, where network applications are typically stream-oriented. Files stored on the hard disk (instead of a streaming tape drive) do not need to be stored sequentially, and Java provides a randomaccessfile class that can randomly access files.
3. A thread is a program execution unit that runs independently of other threads. Java programs may include multiple threads of execution, even if the host actually has only one single CPU, and these threads behave as if they were running on separate (multiple) CPUs.
(1) The Java.lang.Runnable interface defines a run () method as a code block for thread execution. When this method exits, the thread stops running.
(2) The Java.lang.Thread class represents a thread; It defines methods for setting and querying thread attributes, such as execution priority, and for starting a thread's execution.
(3) Synchronized statements and modifiers can be used to write special blocks of code or complete methods that require a thread to obtain a lock before executing the code block or method. This mechanism ensures that two threads do not run code blocks or methods at the same time, preventing different threads from putting shared data in an inconsistent state.
(4) Java.lang.Object's Wait () and notify () methods can be used to suspend a thread and wake it up again.
(5) Thread.yield ()//each thread runs a period of time to provide a running opportunity for other processes so that other processes with the same priority do not "starve"
Example 1: A simple program that shows how to define, manipulate, and run threads
4. If multiple threads can change the state of an object at the same time, you typically must use the Synchronized method or the synchronized statement to ensure that only one thread at a time modifies the state of the object. If not, two threads will eventually overwrite the edits made by the other and cause the object to be in an inconsistent state.
Synchronization also poses a problem when two or more threads are waiting to acquire a lock, and the lock is currently owned by another waiting thread, and a deadlock occurs. To avoid deadlocks, you can require all threads to get all the locks they need in the same order.
Example 2: Demonstrates an error in writing a multithreaded program that intentionally creates a deadlock between two threads.
5. Design a class that may be used for concurrent programs, so that instances of a class may be used by multiple threads at the same time, make sure that such things are linear and secure. One way to ensure thread safety: avoid multiple threads accessing the internal state of an instance concurrently. Because the Java design itself incorporates threads, the Java language provides the synchronized modifier, which is to ensure that the internal state of an instance is not accessed concurrently by multiple threads. When an instance method is declared as synchonized, a thread must obtain a lock on the instance before calling this method. If the lock is already owned by another thread, the previous thread will block until it can get the lock it needs. This ensures that only one thread at a time can invoke an instance of any synchronization method.
Example 3:
Java Step1: Basic knowledge 4