进程:表示一个任务。
线程:一种比进程更小的执行单位,依附在进程中,多个线程共享一个进程的资源。
假设有一项任务是修建一条10公里的马路,需要费用100万元,给某建筑公司修建需要10个月,这可以看作是一个进程;然而我需要更快时间完成这项任务,于是我把这个任务分成子任务由10个建筑公司同时修建,每个公司10万元修建一公里,这可以看作是多个线程。
下面是个示例
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
for (int i=0; i<5; i++) {
new MyThread(i).run();
}
System.out.println("MyThread End");
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 1 and Flag is 1
MyThread with ID 2 and Flag is 2
MyThread with ID 3 and Flag is 3
MyThread with ID 4 and Flag is 4
MyThread End
*/
上面示例没有使用多线程,仅仅是main线程顺序执行.
要使用多线程驱动,可以像下面这样(多线程之间不是顺序执行的,但每个线程仍是顺序执行)
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
for (int i=0; i<5; i++) {
new Thread(new MyThread(i)).start();
}
System.out.println("MyThread End");
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 2 and Flag is 2
MyThread End
MyThread with ID 4 and Flag is 4
MyThread with ID 1 and Flag is 1
MyThread with ID 3 and Flag is 3
*/
我们可以使用java.util.concurrent包中的Executor类来管理线程
Executors.newCachedThreadPool()//创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们
Executors.newFixedThreadPool(int poolSize)//创建一个可重用固定线程数的线程池
Executors.newSingleThreadExecutor()//单一线程,就像是线程数量为1的FixedThreadPool
Executors.newScheduledThreadPool(int corePoolSize)//创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
示例CachedThreadPool:
import java.util.concurrent.*;
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i=0; i<5; i++) {
exec.execute(new MyThread(i));
}
exec.shutdown();
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 1 and Flag is 1
MyThread with ID 3 and Flag is 3
MyThread with ID 2 and Flag is 2
MyThread with ID 4 and Flag is 4
*/
示例ScheduledThreadPool
import java.util.concurrent.*;
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
ExecutorService exec = Executors.newScheduledThreadPool(5);
try{
//设置延后5秒执行
exec.awaitTermination(5000,TimeUnit.MILLISECONDS );
}catch(InterruptedException e) {}
for (int i=0; i<5; i++) {
exec.execute(new MyThread(i));
}
exec.shutdown();
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 1 and Flag is 1
MyThread with ID 2 and Flag is 2
MyThread with ID 4 and Flag is 4
MyThread with ID 3 and Flag is 3
*/
TimeUnit是枚举
public enum TimeUnit {
DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, SECONDS;
}
线程:一种比进程更小的执行单位,依附在进程中,多个线程共享一个进程的资源。
假设有一项任务是修建一条10公里的马路,需要费用100万元,给某建筑公司修建需要10个月,这可以看作是一个进程;然而我需要更快时间完成这项任务,于是我把这个任务分成子任务由10个建筑公司同时修建,每个公司10万元修建一公里,这可以看作是多个线程。
下面是个示例
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
for (int i=0; i<5; i++) {
new MyThread(i).run();
}
System.out.println("MyThread End");
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 1 and Flag is 1
MyThread with ID 2 and Flag is 2
MyThread with ID 3 and Flag is 3
MyThread with ID 4 and Flag is 4
MyThread End
*/
上面示例没有使用多线程,仅仅是main线程顺序执行.
要使用多线程驱动,可以像下面这样(多线程之间不是顺序执行的,但每个线程仍是顺序执行)
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
for (int i=0; i<5; i++) {
new Thread(new MyThread(i)).start();
}
System.out.println("MyThread End");
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 2 and Flag is 2
MyThread End
MyThread with ID 4 and Flag is 4
MyThread with ID 1 and Flag is 1
MyThread with ID 3 and Flag is 3
*/
我们可以使用java.util.concurrent包中的Executor类来管理线程
Executors.newCachedThreadPool()//创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们
Executors.newFixedThreadPool(int poolSize)//创建一个可重用固定线程数的线程池
Executors.newSingleThreadExecutor()//单一线程,就像是线程数量为1的FixedThreadPool
Executors.newScheduledThreadPool(int corePoolSize)//创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
示例CachedThreadPool:
import java.util.concurrent.*;
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i=0; i<5; i++) {
exec.execute(new MyThread(i));
}
exec.shutdown();
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 1 and Flag is 1
MyThread with ID 3 and Flag is 3
MyThread with ID 2 and Flag is 2
MyThread with ID 4 and Flag is 4
*/
示例ScheduledThreadPool
import java.util.concurrent.*;
public class MyThread implements Runnable {
private int flag = 0;
private static int count = 0;
private final int id = count++;
public MyThread(int flag) {
this.flag = flag;
}
public void run() {
System.out.println("MyThread with ID " + id + " and Flag is "+ flag);
}
public static void main(String[] args) {
ExecutorService exec = Executors.newScheduledThreadPool(5);
try{
//设置延后5秒执行
exec.awaitTermination(5000,TimeUnit.MILLISECONDS );
}catch(InterruptedException e) {}
for (int i=0; i<5; i++) {
exec.execute(new MyThread(i));
}
exec.shutdown();
}
}
/*
MyThread with ID 0 and Flag is 0
MyThread with ID 1 and Flag is 1
MyThread with ID 2 and Flag is 2
MyThread with ID 4 and Flag is 4
MyThread with ID 3 and Flag is 3
*/
TimeUnit是枚举
public enum TimeUnit {
DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, SECONDS;
}