Java Basic Tutorial Thread hibernation Java Multithreading tutorial _java

Source: Internet
Author: User

This chapter covers the following topics:
1. Sleep () Introduction
2. Sleep () example
3. Comparison of sleep () and wait ()

1. Sleep () Introduction
Sleep () is defined in Thread.java.
The function of sleep () is to let the current thread hibernate, that is, the current thread will go from "Run state" to "hibernate (blocked) state". Sleep () Specifies the time of hibernation, when the thread sleeps longer than/equal to the sleep time; When the thread is awakened, it becomes "ready" by the blocking state, which waits for the CPU's dispatch to execute.


2. Sleep () example
The following is a simple example to illustrate the use of sleep ().

Copy Code code as follows:

Sleeptest.java's source code
Class Threada extends thread{
Public Threada (String name) {
Super (name);
}
Public synchronized void Run () {
try {
for (int i=0 i <10; i++) {
System.out.printf ("%s:%d\n", This.getname (), i);
I can be divisible by 4, hibernate 100 milliseconds
if (i%4 = 0)
Thread.Sleep (100);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

public class sleeptest{
public static void Main (string[] args) {
Threada T1 = new Threada ("T1");
T1.start ();
}
}

Run Result:

Copy Code code as follows:

t1:0
T1:1
T1:2
T1:3
T1:4
T1:5
T1:6
T1:7
T1:8
T1:9

Results show:
The program is relatively simple, starting thread T1 in main thread main. After the T1 is started, when the calculation I in T1 is divisible by 4, T1 sleeps 100 milliseconds through Thread.Sleep (100).

Comparison of sleep () with wait ()
As we know, the effect of Wait () is to allow the current thread to enter the standby (blocking) state while it is running, releasing the sync lock. The function of sleep () is to allow the current thread to enter the hibernation (blocking) state by running state.
However, wait () releases the synchronization lock of the object, while sleep () does not release the lock.
The following example shows that sleep () does not release the lock.

Copy Code code as follows:

Sleeplocktest.java's source code
public class sleeplocktest{

private static Object obj = new Object ();

public static void Main (string[] args) {
Threada T1 = new Threada ("T1");
Threada t2 = new Threada ("T2");
T1.start ();
T2.start ();
}

Static class Threada extends thread{
Public Threada (String name) {
Super (name);
}
public void Run () {
Get a synchronization lock for an Obj object
Synchronized (obj) {
try {
for (int i=0 i <10; i++) {
System.out.printf ("%s:%d\n", This.getname (), i);
I can be divisible by 4, hibernate 100 milliseconds
if (i%4 = 0)
Thread.Sleep (100);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
}

Run Result:

Copy Code code as follows:

t1:0
T1:1
T1:2
T1:3
T1:4
T1:5
T1:6
T1:7
T1:8
T1:9
t2:0
T2:1
T2:2
T2:3
T2:4
T2:5
T2:6
T2:7
T2:8
T2:9

Results show:
Two threads T1 and T2 are started in main thread main. T1 and T2 in run () refer to the synchronization lock for the same object, that is, synchronized (obj). While the T1 is running, it will call Thread.Sleep (100), but T2 will not get CPU execution power. Because, T1 does not release "the synchronization lock which obj holds"!
Note that if we comment out synchronized (obj) and then execute the program again, T1 and T2 can switch to each other. The following is the source code after the annotation synchronized (obj):

Copy Code code as follows:

Sleeplocktest.java Source code (comment out synchronized (obj))
public class sleeplocktest{

private static Object obj = new Object ();

public static void Main (string[] args) {
Threada T1 = new Threada ("T1");
Threada t2 = new Threada ("T2");
T1.start ();
T2.start ();
}

Static class Threada extends thread{
Public Threada (String name) {
Super (name);
}
public void Run () {
Get a synchronization lock for an Obj object
Synchronized (obj) {
try {
for (int i=0 i <10; i++) {
System.out.printf ("%s:%d\n", This.getname (), i);
I can be divisible by 4, hibernate 100 milliseconds
if (i%4 = 0)
Thread.Sleep (100);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
//            }
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.