原文链接:https://blog.csdn.net/ftell/article/details/79353571#1 线程概念
Java为创建和运行线程以及锁定资源以防止冲突提供了非常好的支持。你可以在程序中创建额外的线程以执行并发任务。在Java中,每个任务都是Runnable接口的实例,也称为一个runnable对象。一个线程实质上是一个对象,它为任务的执行提供便利。
public class TaskClass implements Runnable {
public TaskClass(...) {
}
// 实现Runnable中的run方法
public void run() {
// 告诉系统如何运行自定义线程
}
}
public class Client {
public void someMethod() {
// 创建TaskClass的实例
TaskClass task = new TaskClass(...);
// 创建线程
Thread thread = new Thread(task);
// 启动线程
thread.start();
}
}
task类必须实现 Runnable接口,task必须从线程执行。
task是对象,为了创建task,必须首先为task定义一个实现了Runnable接口的类。Runnable接口相当简单,只包含了一个Run方法。
task必须在线程中执行,Thread类含有用于创建线程的构造函数,以及很多用于控制线程的方法,创建task的线程:
Thread thread = new Thread(task);
然后调用start()方法告诉JVM,线程已经可以运行:
thread.start();
JVM 通过调用task的run()方法执行task. 下面的例子,创建3个线程,分别打印’a’ 100 次,打印’b’ 100 次, 以及打印 0 ~ 100 之间的整数:
public class TaskThreadDemo {
public static void main(String[] args) {
// 创建task
Runnable printA = new PrintChar('a', 100); // Runnable 改成 PrintChar 也可以
Runnable printB = new PrintChar('b', 100); // Runnable 改成 PrintChar 也可以
Runnable print100 = new PrintNum(100); // Runnable 改成 PrintNum 也可以
// 创建线程
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
// 启动线程
thread1.start();
thread2.start();
thread3.start();
}
}
// 打印指定次数字符的 task
class PrintChar implements Runnable {
private char charToPrint; // The character to print
private int times; // The number of times to repeat
/** Construct a task with a specified character and number of
* times to print the character
*/
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
@Override /** Override the run() method to tell the system
* what task to perform
*/
public void run() {
for (int i = 0; i < times; i++) {
System.out.print(charToPrint);
}
}
}
// The task class for printing numbers from 1 to n for a given n
class PrintNum implements Runnable {
private int lastNum;
/** Construct a task for printing 1, 2, ..., n */
public PrintNum(int n) {
lastNum = n;
}
@Override /** Tell the thread how to run */
public void run() {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
}
}
}
Thread 类包含了创建线程的构造函数以及控制线程的方法
Thread 类实现接口 Runnable:
<< interface >>
java.lang.Thread -> java.lang.Runnable
因为Thread实现Runnable,因此可以定义类继承Thread并实现Run方法:
// 自定义thread类
public class CustomThread extends Thread {
public CustomThread(...) {
}
// 重写Runnable里的run方法
public void run() {
// 告诉系统如何执行这个task
}
}
// 自定义类
public class Client {
public void someMethod() {
// 创建一个线程
CustomThread thread1 = new CustomThread(...);
// 启动线程
thread1.start();
// 创建另一个线程
CustomThread thread2 = new CustomThread(...);
// 启动线程
thread2.start();
}
}
例子:并行和串行
//package chapter01;
/**
* 并发和单线程执行测试
*
* @author tengfei.fangtf
* @version $Id: ConcurrencyTest.java, v 0.1 2014-7-18 下午10:03:31 tengfei.fangtf Exp $
*/
public class newtest {
/** 执行次数 */
private static final long count = 40000l;
public static void main(String[] args) throws InterruptedException {
//并发计算
concurrency();
//单线程计算
serial();
}
private static void concurrency() throws InterruptedException {
long start = System.currentTimeMillis();
Thread thread = new Thread(new Runnable() {
@Override
public void run()
{
int a = 0;
for (long i = 0; i < count; i++) {
a += 5;
}
System.out.println(a);
}
});
thread.start(); //启动线程
int b = 0;
for (long i = 0; i < count; i++) {
b--;
}
thread.join();
long time = System.currentTimeMillis() - start;
System.out.println("concurrency :" + time + "ms,b=" + b);
}
private static void serial() {
long start = System.currentTimeMillis();
int a = 0;
for (long i = 0; i < count; i++) {
a += 5;
}
int b = 0;
for (long i = 0; i < count; i++) {
b--;
}
long time = System.currentTimeMillis() - start;
System.out.println("serial:" + time + "ms,b=" + b + ",a=" + a);
}
}