Java Threads:
A thread is a sequential control flow within a program.
The CPU is actually at a point in time and executes only one. Just because we split the CPU into multiple time slices, we look like multiple threads because of the speed.
Just like your time is divided into several pieces, so the whole seems to do things regularly, efficiency will be high, not to mention the CPU.
Thread creation and startup:
Method One:
Java threads are implemented through the Java.lang.Thread class.
When the VM starts, it has a thread defined by the main method.
Each thread is the tread object through its run () method to complete the operation.
Start thread mode: Thread's Start () method.
The following code:
public
class
TestThread
{
public
static
void
main(String[] args)
{
Runner1 r =
new
Runner1();
//定义线程对象。并且调用线程的构造方法。
Thread t =
new
Thread(r);
//启动线程。
t.start();
for
(
int
i=
0
;i<
100
;i++)
{
System.out.println(
"Main thread-----"
+ i);
}
}
}
class
Runner1
implements
Runnable
{
public
void
run()
{
for
(
int
i=
1
;i<
100
;i++)
{
System.out.println(
"Runner1"
+ i);
}
}
}
Note:The override (override) run () method is invoked by the start () method of the thread, and the JVM automatically calls the Run method to perform the task, but the overload run () method, like the normal member method, does not invoke the thread's start () Method and is automatically run by the JVM.
Method Two:
Define a subclass of the thread class, override the Run () method, and then generate an object of this class at the START process.
The following code:
public
class
TestThread
{
public
static
void
main(String[] args)
{
Runner1 r =
new
Runner1();
//启动线程。
r.start();
for
(
int
i=
0
;i<
100
;i++)
{
System.out.println(
"Main thread-----"
+ i);
}
}
}
class
Runner1
extends
Thread
{
public
void
run()
{
for
(
int
i=
1
;i<
100
;i++)
{
System.out.println(
"Runner1"
+ i);
}
}
}
here are the methods and properties of the thread:
method of Thread, property
1) Priorities (priority)Each class has its own priority, the general property is represented by an integer of 1-10, the default priority is 5, the priority is the highest is 10, the high priority thread is not necessarily higher than the priority of the thread, but the probability of execution is high; The priority of the default thread is the same as the thread that created it;
2) Thread.Sleep ()/sleep (long Millis)The time at which the current thread sleeps/millis (Millis Specifies that the sleep time is its minimum non-execution time, since sleep (Millis) is not guaranteed to be dispatched immediately by the JVM when Sleep arrives), and sleep () is a static method, So he won't stop the other threads are also dormant; the thread sleep () does not lose the owning object lock. Function: Keep the object lock, give up the CPU, call the purpose is not to let the current thread alone occupy the CPU resources obtained by the process, to leave a certain time for other threads to execute the opportunity; 3) Thread.yield () gives up the use of the CPU to execute opportunities for other threads, Allow the same priority thread to run (but does not guarantee that the current thread will be dispatched again by the JVM, bringing the thread back into the running state), and the yield () method will not work if there are no equal priority threads.
4) Thread.Join ()The thread that uses the method executes after the execution is complete. 5) object.wait () when a thread executes to the wait () method, he enters a waiting pool (waiting pool) associated with the object, loses the machine lock on the object-temporarily, and returns the object lock after wait. The current thread must have a lock on the current object, and if the current thread is not the owner of the lock, the illegalmonitorstateexception exception is thrown, so wait () must be called in the synchronized block. 6) Object.notify ()/notifyall () wakes up the first thread/all threads waiting in the current object waiting pool. Notify ()/notifyall () must also have the same object lock, or a Illegalmonitorstateexception exception will be thrown. 7) Synchronizing blocksynchronized block/Method Controls access to class member variables; Each object in Java has a unique, built-in lock, with each synchronized block/ Method can only be accessed by holding a lock that calls the method's locked object, otherwise the owning thread is blocked, the machine lock is exclusive, and once it is held by a thread, the other thread can no longer have (no access to other synchronization methods), and once the method executes, the lock is exclusive until it is returned from the method. , the blocked thread can then get the lock and re-enter the executable state.
Simple implementation and method of Java threading