Java Thread/Runnable线程基础
写在前面
一般 Java 开发中,Java 线程因为使用场景少,里面的对象敏感而抽象,学习起来,比较吃力…
线程的创建比较重要,不同的创建方式,好的创建方式,可更好的管理线程,状态,监控等…我们的线程不该是 Hello World不是简单的 new 就可以…
一、五种创建方式
1.1、
import java.util.concurrent.TimeUnit;
public class CreatingThreadsFirstWay {
public static void main (String[] args) {
System.out.println("Starting main!!!!");
new FirstWay();
Thread thread = new FirstWay();
System.out.println("Ending main!!!");
}
}
class FirstWay extends Thread {
private static int count = 1;
private int id;
@Override
public void run() {
System.out.println("Starting First Way -------");
for(int i = 1; i <= 10; i++) {
System.out.println("<"+ id + "> Count: " +i);
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Ending First Way -------");
}
public FirstWay() {
id = count++;
this.start();
}
}
1.2、
public class CreatingThreadsSecondWay {
public static void main (String[] args) {
System.out.println("Starting main!!!!");
// new SecondWay().start();
Thread thread = new FirstWay();
thread.start();
System.out.println("Ending main!!!");
}
}
class SecondWay extends Thread {
private static int count = 1;
private int id;
@Override
public void run() {
System.out.println("Starting First Way -------");
for(int i = 1; i <= 10; i++) {
System.out.println("<"+ id + "> Count: " +i);
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Ending First Way -------");
}
public SecondWay() {
id = count++;
}
}
1.3、
public class CreatingThreadsThirdWay {
public static void main (String[] args) {
System.out.println("Starting main.......");
new ThirdWay();
new ThirdWay();
System.out.println("Ending main.......");
}
}
class ThirdWay implements Runnable {
static int count = 1;
int id = 0;
public void run() {
for(int i = 1 ; i <= 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("<"+id+"> Tick Tick: " + i);
}
}
ThirdWay() {
this.id = count++;
new Thread(this).start();
}
}
1.4、
public class CreatingThreadsFourthWay {
public static void main (String[] args) {
System.out.println ("Starting Main..........");
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(new FourthWay());
executorService.submit(new FourthWay());
System.out.println ("Ending Main..........");
}
}
class FourthWay implements Runnable {
// 用来监视线程数量
static int count = 1;
int id = 0;
public void run() {
for(int i = 1 ; i <= 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("<"+id+"> Tick Tick: " + i);
}
}
FourthWay() {
this.id = count++;
}
}
1.5、
public class CreatingThreadsFifthWay {
public static void main (String[] args) {
System.out.println("Starting Main........");
Thread t = new Thread(new Runnable() {
public void run() {
for(int i = 1 ; i <= 10; i++) {
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Tick Tick: " + i);
}
}
});
t.start();
System.out.println("Ending Main........");
}
}
二、比较区别
待记录