One-thread Concept
Process: it is a program in progress, and each process has an execution order. This sequence is an execution path or a control unit.
Thread: it is an independent control unit in the process. The thread controls the execution of the process.
When the Java Virtual Machine starts, there will be a process java.exe. At least one thread in the process is responsible for Java program execution. The code run by this thread exists in the main method. This thread is called the main thread, and each process has at least one thread.
Creation of two threads
Method 1: Create a thread to inherit the Thread class,
Steps:
1. Define class to inherit from thread.
2. Rewrite the run method in the Thread class.
Objective: To define your own task to drive the thread.
3. Call the start method of the thread. This method has two functions: Start the thread and call the run method.
The following code:
Class mythread extends thread
{
Public void run ()
{
For (INT I = 0; I <100; I ++)
System. Out. println ("thread1 run ----" + x );
}
}
Class mythreaddemo
{
Public static void main (string [] ARGs)
{
// For (INT x = 0; x <4000; X ++)
// System. Out. println ("Hello world! ");
Mythread T = new mythread (); // create a thread.
// T. Start (); // enable the thread and execute the run method of the thread.
T. Run (); // only indicates the method of calling an object. The thread is created and does not run.
For (INT I = 0; I <60; I ++)
System. Out. println ("Hello world! -- "+ I );
}
}
Implement the runnable interface to implement the run method in the interface
Steps:
1. The design class implements the runnable interface to implement the run method in this interface;
2. The class is created for two years as the construction method for passing parameters such as the Thread class;
The following code:
Class mythread implements runnable {
Public void run (){
// Define your own task here
}
}
Then, create a startup thread.
New (New mythread (). Start ();
Inherit the Thread class:
(1) Advantages: simple writing. If you need to access the current thread, you can obtain the current thread without using the thread. currentthread () method and directly using this.
(2) Disadvantage: Because the Thread class has inherited the Thread class, it cannot inherit other parent classes.
Runnable interface implementation:
(1) advantage: the Thread class only implements the runable interface and can inherit other classes. In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to process the same resource, so that the CPU code and data can be separated, the formation of a clear model better reflects the idea of object-oriented.
(2) Disadvantages: programming is a little complicated. To access the current thread, you must use the thread. currentthread () method.
Objects are like locks. The thread holding the lock can be executed in synchronization.
The thread that does not hold the lock cannot even obtain the execution right of the CPU, because the lock is not obtained.
Finally, we will summarize the singleton design mode:
Lazy design patterns are as follows:
Class Single
{
Private Static Single S = NULL;
Private single (){}
Public staticSingle getinstance ()
{
If (S = NULL)
// -- Mark
S = new single ();
}
}
Return S;
}
There are security risks in lazy design models, when the thread executes at a, the execution right is denied and stops. At this time, another thread may execute here, so that multiple single objects may be created, causing an error; this design must process the following code simultaneously:
Public staticSingle getinstance ()
{
If (S = NULL)
{
Synchronized (single. Class)
{
If (S = NULL)
S = new single ();
}
}
Return S;
}
}
Another design mode is the hungry Chinese style. This design is highly secure.
Class Single
{
Private Static final single S = new single ();
Private single (){}
Public static single getinstance ()
{
Return S;
}
}
ThuThread security issues
Prerequisites for synchronization:
1. There must be two or more threads.
2. Multiple Threads must use the same lock.
Ensure that only one thread is running in synchronization.
Benefit: it solves the security problem of multithreading.
Disadvantages: multiple threads need to judge the lock, which consumes more resources.
The lock object is an instance of the object. You can also use the lock function to synchronize the Lock Object at this time by calling this function, static synchronization method, the lock used is the bytecode file object of the class of the method. Class Name. Class, the following code:
In multi-thread programming, errors often occur. This is because the system thread scheduling is random, so we must synchronize critical resources. The following is a classic example of bank money Acquisition.
Procedure:
1. the user enters the account and password system to determine whether the user's account and password match.
2. Enter the withdrawal amount.
3. The system determines whether the account balance is greater than the withdrawal amount.
4. If the value is greater than the value, the operation succeeds. Otherwise, the operation fails.
We write the withdrawal program according to the above process. We use two threads to simulate the money withdrawal operation:
Public class account
{
// Encapsulate two attributes: account ID and account balance
Private string accountno;
Private double balance;
Public Account (){}
// Constructor
Public Account (string accountno, double balance)
{
This. accountno = accountno;
This. Balance = balance;
}
Public void setaccountno (string accountno)
{
This. accountno = accountno;
}
Public String getaccountno ()
{
Return this. accountno;
}
Public void setbalance (double balance)
{
This. Balance = balance;
}
Public double getbalance ()
{
Return this. balance;
}
// The following two methods calculate the account hashcode Based on accountno and determine equals
Public int hashcode ()
{
Return accountno. hashcode ();
}
Public Boolean equals (Object OBJ)
{
If (OBJ! = NULL
& Obj. getclass () = Account. Class)
{
Account target = (account) OBJ;
Return target. getaccountno (). Equals (accountno );
}
Return false;
}
}
Next, we will provide a Thread class for obtaining money, execute an account, and perform operations on the amount of money to withdraw. The logic is that when the balance is insufficient, the cash cannot be extracted. When the balance is sufficient, the bill will be spit out and the balance will be reduced.
Public class drawthread extends thread
{
// Simulate a user account
Private Account account;
// The amount of money that the thread wants to fetch currently
Private double drawamount;
Public drawthread (string name, account Account account,
Double drawamount)
{
Super (name );
This. Account = Account;
This. drawamount = drawamount;
}
// When multiple threads modify the same shared data, data security issues are involved.
Public void run ()
{
// The account balance is greater than the amount of money received
If (account. getbalance ()> = drawamount)