Concurrent programming: A programmatic way to enable multiple tasks to run concurrently.
Process: A running program that has its own separate piece of memory space, and the internal data and state of each process are completely independent. You can run two or more programs at the same time.
Threads: Each task is called a thread, and a program that runs multithreaded within a program is called a multithreaded program. A thread is a single sequential control flow within a process, and is the smallest processing unit.
To create a thread:
1. Inherit the thread class, rewrite the Run () method, and write the code to execute in the run () method. The thread object is created first, and the start () thread is called by the dot call.
2. Implement the Runable interface, only the run () method. The main () method is the main thread.
Priority: Up to 10, lowest 1, default 5.
Thread synchronization: Startup Object Lock (Synchronized) uses synchronous blocks of code, using synchronous methods.
Deadlock: Two threads are hungry, waiting for the other person to release the object lock, causing each other to be unable to execute down.
Cases:
public class multithreaddemo{
public static void Main (String [] args) {
thread T1 = new Thread (new MyTask (), "Thread A");
T1.start ();
Thread t2 = new Thread (new MyTask (), "thread B");
T2.start ();
System.out.println ();
}
}
Class MyTask implements Runnable {
public void Run () {
for (int i = 0; i < 5; i++) {
The I value of System.out.println (Thread.CurrentThread (). Gtname () + "is:" +i);
}
}
}
Java Learning Notes (12) Multithreaded programming