Java Thread.run() VS Thread.star
2018-01-31 本文已影响17人
JaedenKil
Each thread starts in a separate call stack.
Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.junit.jupiter.api.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ThreadTest {
@BeforeAll
static void setupBeforeClass() {
System.out.println("BeforeAll executed.");
printTimestamp();
}
@BeforeEach
void setUp() {
System.out.println("BeforeEach is executed.");
}
@Test
void test01() throws InterruptedException {
System.out.println("test01");
//Sleep
System.out.println("Sleep 5 seconds.");
Thread.sleep(5000);
}
@Test
void test02() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
System.out.println("test02");
//New thread sleep 20 seconds
Thread check = new Thread() {
public void run() {
for (int x = 1; x <= 10; x ++) {
try {
Thread.sleep(1000);
System.out.println("This is the inside thread(" + x + ").");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latch.countDown();
}
};
check.run();
// NewThreadClass ntc = new NewThreadClass();
// ntc.run();
//Sleep
System.out.println("Sleep 5 seconds.");
//Thread.sleep(5000);
for (int x = 1; x <= 5; x ++) {
Thread.sleep(1000);
System.out.println("This is the outside thread(" + x + ").");
}
latch.await();
}
@AfterEach
void tearDown() {
System.out.println("AfterEach executed.");
}
@AfterAll
static void tearDownAfterClass() {
System.out.println("AfterAll executed.");
printTimestamp();
}
private static void printTimestamp() {
Long ts = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = new Date(ts);
System.out.println("Date: " + sdf.format(date));
}
}
"C:\Program Files\Java\jdk1.8.0_152\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=65214:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\zzheng\IdeaProjects\ThreadTest\out\production\ThreadTest;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-jupiter-api-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\opentest4j-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-jupiter-engine-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-commons-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-engine-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-launcher-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-runner-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-suite-api-1.0.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 ThreadTest
BeforeAll executed.
Date: 2018-01-31 10:28:34:032
BeforeEach is executed.
test01
Sleep 5 seconds.
AfterEach executed.
BeforeEach is executed.
test02
This is the inside thread(1).
This is the inside thread(2).
This is the inside thread(3).
This is the inside thread(4).
This is the inside thread(5).
This is the inside thread(6).
This is the inside thread(7).
This is the inside thread(8).
This is the inside thread(9).
This is the inside thread(10).
Sleep 5 seconds.
This is the outside thread(1).
This is the outside thread(2).
This is the outside thread(3).
This is the outside thread(4).
This is the outside thread(5).
AfterEach executed.
AfterAll executed.
Date: 2018-01-31 10:28:54:093
Process finished with exit code 0
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.junit.jupiter.api.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ThreadTest {
@BeforeAll
static void setupBeforeClass() {
System.out.println("BeforeAll executed.");
printTimestamp();
}
@BeforeEach
void setUp() {
System.out.println("BeforeEach is executed.");
}
@Test
void test01() throws InterruptedException {
System.out.println("test01");
//Sleep
System.out.println("Sleep 5 seconds.");
Thread.sleep(5000);
}
@Test
void test02() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
System.out.println("test02");
//New thread sleep 20 seconds
Thread check = new Thread() {
public void run() {
for (int x = 1; x <= 10; x ++) {
try {
Thread.sleep(1000);
System.out.println("This is the inside thread(" + x + ").");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latch.countDown();
}
};
check.start();
// NewThreadClass ntc = new NewThreadClass();
// ntc.run();
//Sleep
System.out.println("Sleep 5 seconds.");
//Thread.sleep(5000);
for (int x = 1; x <= 5; x ++) {
Thread.sleep(1000);
System.out.println("This is the outside thread(" + x + ").");
}
latch.await();
}
@AfterEach
void tearDown() {
System.out.println("AfterEach executed.");
}
@AfterAll
static void tearDownAfterClass() {
System.out.println("AfterAll executed.");
printTimestamp();
}
private static void printTimestamp() {
Long ts = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = new Date(ts);
System.out.println("Date: " + sdf.format(date));
}
}
"C:\Program Files\Java\jdk1.8.0_152\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=49212:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\zzheng\IdeaProjects\ThreadTest\out\production\ThreadTest;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-jupiter-api-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\opentest4j-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-jupiter-engine-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-commons-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-engine-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-launcher-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-runner-1.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-platform-suite-api-1.0.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 ThreadTest
BeforeAll executed.
Date: 2018-01-31 10:41:58:923
BeforeEach is executed.
test01
Sleep 5 seconds.
AfterEach executed.
BeforeEach is executed.
test02
Sleep 5 seconds.
This is the outside thread(1).
This is the inside thread(1).
This is the outside thread(2).
This is the inside thread(2).
This is the outside thread(3).
This is the inside thread(3).
This is the inside thread(4).
This is the outside thread(4).
This is the inside thread(5).
This is the outside thread(5).
This is the inside thread(6).
This is the inside thread(7).
This is the inside thread(8).
This is the inside thread(9).
This is the inside thread(10).
AfterEach executed.
AfterAll executed.
Date: 2018-01-31 10:42:13:987
Process finished with exit code 0
public class TestThread02 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
};
t1.start();
t2.start();
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
}
"C:\Program Files\Java\jdk1.8.0_152\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=52151:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\zzheng\IdeaProjects\ThreadTest\out\production\ThreadTest;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-jupiter-api-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\opentest4j-1.0.0.jar" TestThread02
Thread-0: 1
main: 1
Thread-1: 1
Thread-1: 2
Thread-0: 2
main: 2
Thread-0: 3
main: 3
Thread-1: 3
Thread-0: 4
main: 4
Thread-1: 4
main: 5
Thread-0: 5
Thread-1: 5
Process finished with exit code 0
public class TestThread02 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
};
t1.run();
t2.run();
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
}
"C:\Program Files\Java\jdk1.8.0_152\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=52171:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\zzheng\IdeaProjects\ThreadTest\out\production\ThreadTest;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\junit-jupiter-api-5.0.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\plugins\junit\lib\opentest4j-1.0.0.jar" TestThread02
main: 1
main: 2
main: 3
main: 4
main: 5
main: 1
main: 2
main: 3
main: 4
main: 5
main: 1
main: 2
main: 3
main: 4
main: 5
Process finished with exit code 0