1. The difference between start () and run ()
Start () is the real implementation of multi-threading, because start will let the thread in a ready state, without waiting for run to finish running the following code, and if the direct call to run is a normal method call, the program is executed sequentially
2. Which method to use to create a thread better
Java can only be single-inheritance but can be implemented, so if you want to inherit another class, it is necessary to choose to implement the Runnable interface mode
3, callable and runnable differences
callable can return a value and throw an exception
the specific implementation is
//callable is a generic interface
Public class Test implements callable<string> {
Public static void Main (string[] args) throws Executionexception, interruptedexception {
//Create callable Object
callable<string> callable = new Test ();
//Create Futuretask object, same as get return result
futuretask<string> task = new futuretask<> (callable);
//futuretask is an implementation class for runnable, so you can create threads as parameters
New Thread (Task). Start ();
//Call Get () to block the main thread and get the result returned
String result = Task.get ();
System.out.println ("Hello:" + result);
}
@Override
Public String Call () throws Exception {
Thread.Sleep (+);
return "Lalala";
}
}
4. Race Condition:
When two or more processes or threads read or write to the shared data, the final result depends on the order in which those processes or threads are executed. The general race condition means that the thread is unsafe
5, why wait and notify method to use in the synchronization block?
Because not doing so throws an exception, and in order to avoid a race condition
6. Stack and heap differences
Stacks are used to store local variables, primarily for executing programs, fast access, size and lifetime must be determined, lack of flexibility
The heap is used to store class variables and the instance variables created by new, primarily for storing objects, slow access, dynamically allocating memory at run time, and lifetime without the need to determine in advance
7, Volative and CONST,STRICTFP
Volatile variable is that this variable may be unexpectedly changed
Const is reserved word, constant meaning, reserved word is now useless, future upgrade version may be upgraded as keyword
STRICTFP keywords can be applied to classes, interfaces, methods, and all float,double strictly follow the FP-STRICT specification.
8. Thread pool
Threadpoolexecutor creating threads takes resources and time, and the thread pool can create a number of threads to respond to processing when the program is started, called the thread pool
9, Linkedblockingqueue
is a one-way list implemented by the blocking queue, in FIFO order. Supports multi-threaded concurrency operations.
Put () inserts an element into the tail of the queue
Peek () gets but does not remove the header of this queue, and null returns NULL
Poll () gets and removes the header of this queue, and null returns NULL
Clear () Removes all elements
Take () gets and removes the header of this queue and waits until the element is available
10, thread safety is that multiple threads run this code at the same time, and the result is the same as single-threaded run results
Vector,linkedlist,hashtable in the collection, is thread-safe
11, how to let the thread stop
Use Isinterrupted () to determine if the interrupt bit is true
Set the interrupt bit to true with interrupt ()
12. Conditions for the birth and Death lock
(1) Mutual exclusion: A resource can only be used by one thread at a time
(2) Request and hold: A line routines to request resources and keep the acquired resources
(3) No deprivation: for the resources that others have got, cannot forcibly deprive
(4) Cyclic wait: the relationship between multiple threads forming a cycle waiting resource
How to resolve:
It is easiest to stop the loop waiting, lock in order, and give the lock an orderly order, only the thread holding the front lock can hold the back lock.
13, the communication between the threads how to achieve, the most basic is the use of multiple objects
Inductive to sleep lock out, awake locked inside, sleep with the object of their own, wake up the object of sleep
Example
Public class Test implements Runnable {
private String name;
private Object prev;
private Object self;
Private Test (String name, Object prev, object self) {
this.name = name;
This.prev = prev;
this.self = self;
}
@Override
Public void Run () {
int count = ten;
While (Count > 0) {
synchronized (prev) {
synchronized (self) {
System.out.print (name);
count--;
self.notify ();
}
try {
prev.wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
}
}
Public static void Main (string[] args) throws Exception {
Object A = new object ();
Object B = new Object ();
Object c = new Object ();
//Each thread requires 2 objects to form 2 behaviors, an object is responsible for waking up the next thread, and an object is responsible for getting the thread to sleep, so how many threads are required to have the number of object
Test PA = new Test ("a", C, a);
Test PB = new Test ("B", A, b);
Test PC = new Test ("C", B, c);
New Thread (PA). Start ();
Thread.Sleep (100);//Ensure that the order a, B, C is executed
new Thread (Pb). Start ();
Thread.Sleep (+);
new Thread (PC). Start ();
Thread.Sleep (+);
}
}
Multi-threaded Learning