1. Process:
(i) The procedure being implemented is described as a process. The process is responsible for dividing the memory space.
(b) Problem: Windows claims to be a multitasking operating system, so is Windows running multiple applications at the same time?
From a macro perspective: Windows is really running multiple applications at the same time.
From the microscopic point of view: The CPU is doing a quick switch to perform the action, because of the speed attitude, so I do not feel in the switch only.
2. Threads:
The priority of the thread is 5 by default;
(a) The thread is responsible for the execution of the code in a process, which is an execution path in the process,
(b) Multithreading: There are multiple threads in a process that perform different tasks at the same time.
(c) Question: A. The execution of the code is thread-responsible, so does his execution process?
Running any Java program, the JVM will create a main thread to execute all the code in the main method when it runs.
B. A Java application has at least a few threads?
There are at least two threads, one is the main thread responsible for the execution of the Main method code, one is the garbage collector thread, which is responsible for garbage collection.
(iv) The benefits of Multithreading:
1. Resolves a problem where a process can perform multiple tasks at the same time.
2. Increased utilization of resources.
(v) The disadvantages of multithreading:
1. Increase the burden on the CPU.
2. Reduces the execution probability of a thread in a process.
3. Thread-safety issues are raised.
4. There is a deadlock phenomenon.
(vi) How to create Multithreading:
A. How to create a thread:
Way One:
1. Customize a class to inherit the thread class.
2. Rewrite the thread class's Run method to write the task code of the custom thread in the Run method
Question: What is the purpose of overriding the Run method?
Each thread has its own task code, the task code of the main thread created by the JVM is all the code in the Main method, and the custom thread's task code is written in the Run method, and the custom thread is responsible for the code in the Run method.
3. Create a child class object for thread, and call the Start method to open the thread.
Note: Once a thread is turned on, the thread executes the code in the Run method, and the run method must not be called directly, and calling the Run method calls a normal method rather than opening a new thread.
(vii) Creating multithreaded examples
Public classMyThreadextendsthread{@override//rewrite the thread's Run method, and then call this custom thread Public voidrun () { for(inti=0;i<10;i++) {System.out.println ("Custom Thread:" +i); } } Public Static voidMain (string[] args) {//to create a custom thread classDemo d=NewDemo (); //call the Start method to enable the thread and cannot be called through the D.run () method, so that no thread is startedD.start (); for(inti=0;i<10;i++) {System.out.println ("Main thread:" +i); } } }
View Code
(eight) thread safety (very important)
A. Multi-threaded ticketing, there are several threads together to arrive at the location of the problem this is a thread safety problem
B. Workaround: The principle of thread security is that multiple threads simultaneously access a location within the code block that should not be accessed concurrently, and we make that part of the code a whole that can only be accessed by one thread at a time, which solves the problem.
Root cause of thread safety issues:
1. There are two or more than two thread objects, and a resource is shared between threads.
2. There are multiple statements that operate a shared resource
Mode one: Synchronizing code blocks
Synchronized (lock object) {
Code that needs to be synchronized ...
}
Note: 1. Any object can be used as a lock object
Cause: Any object can be used as a lock object because a state is maintained within the object,
The synchronization mechanism of Java is the identification of the state in the object as the lock
state=0: Open; 1 off
2. Calling the sleep method in a synchronous code block does not release the object
3. Efficiency is reduced only if you really need to synchronize blocks of code
4. The lock object for multi-threaded operation must be a unique share, otherwise invalid
C. Example: A bank account of 5000, two couples with a passbook, one holding a card, start to take the money game, each time can only take 1000 pieces, require no thread safety issues.
classSaleticketextendsthread{Static intnum = 50;//votes are non-static member variables, and non-static member variable data is maintained for each object in one piece of data. StaticObject o =NewObject (); PublicSaleticket (String name) {Super(name); } @Override Public voidrun () { while(true){ //Synchronizing code blocks synchronized("Lock") { if(num>0) {System.out.println (Thread.CurrentThread (). GetName ()+ "SOLD" +num+ "Ticket"); Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } num--; }Else{System.out.println ("It's sold out."); Break; } } } } } Public classDemo4 { Public Static voidMain (string[] args) {//create three thread objects, simulate three windowsSaleticket Thread1 =NewSaleticket ("Windows 1"); Saleticket thread2=NewSaleticket ("Windows 2"); Saleticket thread3=NewSaleticket ("Windows 3"); //turn on thread ticketingThread1.start (); Thread2.start (); Thread3.start (); } }
View Code
Get started with Java 06