1.线程的3种实现方式
package com.test.web.controller;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class TestThread {
public static void main(String[] args) {
//继承Thread类
MyThread myThread = new MyThread();
myThread.start();
//实现runnable接口
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
//实现Callable接口
MyCallable myCallable = new MyCallable();
FutureTask<Integer>futureTask = new FutureTask<Integer>(myCallable);
Thread t = new Thread(futureTask);
t.start();
try {
int sum = futureTask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class MyThread extends Thread{
@Override
public void run(){
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
}
}
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
return null;
}
}
2.线程的状态转换
3.线程的基本控制方法
方法 | 功能 |
---|---|
isAlive() | 判断是线程是否终止 |
getPriority() | 得到线程的优先级数值 |
setPriority() | 设置线程的优先级数值 |
Thread.sleep() | 将当前线程置顶睡眠的毫秒数 |
join() | 调用某线程的join方法,将当前线程与该线程合并,即等待该线程结束,再回复当前线程的运行 |
wait() | 当前线程进入对象的wait pool |
yield() | 让出cpu,让当前线程进入就绪队列等待调度 |
notify()/notifyAll() | 唤醒对象的wait pool中的一个/所有的等待线程 |
4.方法示例
1.sleep()方法
public class TestThread {
public static void main(String[] args) {
//继承Thread类
MyThread myThread = new MyThread();
myThread.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myThread.interrupt();
}
}
class MyThread extends Thread{
@Override
public void run(){
while(true){
System.out.println("==="+new Date()+"===");
try {
Thread.sleep(1000);//这里只能写try catch来捕获异常,子类不能抛出和父类不同的异常
} catch (InterruptedException e) {
return;
}
}
}
}
}
sleep()方法只要被打断,就一定会抛出异常,但是interrupt()这个方法比较粗暴,当线程正在打开一个文件流的时候,调用interrupt()方法,结束线程,那么当前的文件流就会永远的打开。下面的方法就优化了上面方法的缺陷:定义一个私有的变量,和一个public方法,调用这个public方法来终止线程的运行。
public class TestThread {
public static void main(String[] args) {
//继承Thread类
MyThread myThread = new MyThread();
myThread.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myThread.stopSleep();
}
}
class MyThread extends Thread{
private boolean flag = true;
@Override
public void run(){
while(flag){
System.out.println("==="+new Date()+"===");
try {
Thread.sleep(1000);//这里只能写try catch来捕获异常,子类不能抛出和父类不同的异常
} catch (InterruptedException e) {
return;
}
}
}
public void stopSleep(){
flag = false;
}
}
}
2.join方法
public class TestThread {
public static void main(String[] args) {
//继承Thread类
MyThread myThread = new MyThread("abcd");
myThread.start();
try {
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i =0;i<=10;i++){
System.out.println("i am main Thread");
}
}
}
class MyThread extends Thread{
MyThread(String s){
super(s);
}
@Override
public void run(){
for(int i =0;i<=10;i++){
System.out.println("i am "+ getName());
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3.yield()方法
yield()方法比较高风亮节,当线程调用 yield()方法时候,就会让出当前的时间片,给其他的线程来执行
public class TestThread {
public static void main(String[] args) {
//继承Thread类
MyThread myThread1 = new MyThread("t1");
MyThread myThread2 = new MyThread("t2");
myThread1.start();
myThread2.start();
}
}
class MyThread extends Thread{
MyThread(String s){
super(s);
}
@Override
public void run(){
for(int i =0;i<=100;i++){
System.out.println(getName()+":"+i);
if(i%10 == 0){
yield();
}
}
}
}