本篇介绍Java多线程。能够并发执行任务。但要注意的是线程创建太多,会导致CPU 花费在上下文的切换的时间将多于执行程序的时间。程序执行效率反而降低。
创建线程方式有以下3种
1.通过实现Runnable接口创建线程;
2.通过继承Thread类创建线程;
3.通过 Callable 和 Future 创建线程;
package com.example.javatest;
/**
* Author:W
* 通过实现Runnable接口来实现
*/
public class Thread1 implements Runnable {
private Thread thread;
private String threadName;
public Thread1(String threadName)
{
this.threadName = threadName;
System.out.println("线程"+threadName+" :创建成功!");
}
@Override
public void run() {
System.out.println("线程"+threadName+" :正在运行。。。");
try
{
for (int i=0;i<3;i++)
{
System.out.println("线程"+threadName+" 正在运行。。。打印:"+ i);
Thread.sleep(50);
}
}catch (InterruptedException e)
{
System.out.println("线程" + threadName + " 异常中断");
}
System.out.println("线程" + threadName + " :结束退出");
}
//线程开始
public void start()
{
System.out.println("线程"+threadName+" :开始");
if (thread == null)
{
thread = new Thread(this,threadName);
thread.start();
}
}
}
package com.example.javatest;
/**
* Author:W
* 通过继承Thread类创建线程
*/
public class Thread2 extends Thread {
public Thread thread;
public String threadName;
public Thread2(String threadName)
{
this.threadName = threadName;
System.out.println("线程"+threadName+" :创建成功!");
}
//线程开始执行
public void run()
{
System.out.println("线程"+threadName+" :正在运行。。。");
try
{
for (int i=0;i<2;i++)
{
System.out.println("线程"+threadName+" 正在运行。。。打印:"+ i);
Thread.sleep(50);
}
}catch (InterruptedException e)
{
System.out.println("线程" + threadName + " 异常中断");
}
System.out.println("线程" + threadName + " :结束退出");
}
//线程开始
public void start()
{
System.out.println("线程"+threadName+" :开始");
if (thread == null)
{
thread = new Thread(this,threadName);
thread.start();
}
}
}
package com.example.javatest;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Author:W
* 通过 Callable 和 Future 创建线程
*/
public class Thread3 implements Callable<Integer> {
public Thread thread;
public String threadName;
public Thread3(String threadName)
{
this.threadName = threadName;
System.out.println("线程"+threadName+" :创建成功!");
}
@Override
public Integer call() throws Exception {
System.out.println("线程"+threadName+" :正在运行。。。");
int a = 0;
for (int i=0;i<5;i++)
{
a = i;
System.out.println("线程"+threadName+" 正在执行。。。打印:"+i);
}
System.out.println("线程" + threadName + " :结束退出");
return a;
}
public void start()
{
FutureTask<Integer> futureTask = new FutureTask<Integer>(this);
if (thread == null)
{
thread = new Thread(futureTask,threadName);
thread.start();
}
try
{
System.out.println("线程"+threadName+"执行任务完成返回值:"+ futureTask.get());
}catch (InterruptedException e)
{
e.printStackTrace();
}catch (ExecutionException e)
{
e.printStackTrace();
}
}
}
线程测试脚本
package com.example.javatest;//包名定义
/**
* Author:W
* 多线程创建使用:并发执行任务。注意:线程创建太多,会导致CPU 花费在上下文
* 的切换的时间将多于执行程序的时间。程序执行效率反而降低。
* 1.通过实现Runnable接口创建线程
* 2.通过继承Thread类创建线程
* 3.通过 Callable 和 Future 创建线程
*/
public class MainTest{
public static void main(String[] args)
{
System.out.println("===1.通过实现Runnable接口创建线程===");
Thread1 thread1 = new Thread1("Runnable");
thread1.start();
System.out.println("===2.通过继承Thread类创建线程===");
Thread2 thread2 = new Thread2("Thread");
thread2.start();
System.out.println("===3.通过 Callable 和 Future 创建线程===");
Thread3 thread3 = new Thread3("Callable");
thread3.start();
}
}
运行结果如下: