Java创建线程的三种方式
- 继承Java的Thread类
继承Thread类,在子类中重写run()方法,实现线程处理的内容,然后创建子类实例,调用start()方法启动线程 Thread类常用方法: Thread currentThread():获取当前线程实例; String getName():获取线程名称; setName():设置线程名称; sleep():休眠线程
import java.io.*;
import java.util.*;
import java.lang.*;
public class SubThread extends Thread {
private String[] tikets;
public SubThread(){
}
public SubThread(String[] tikets){
this.tikets = tikets;
}
public void getTikets(String[] tikets){
this.tikets = tikets;
}
public String[] getTikets(){
return this.tikets;
}
public void run() {
if (this.tikets == null || this.tikets.length == 0){
System.out.println("没有可出的票据,请输入票据重新启动!");
}else{
String threadname = Thread.currentThread().getName();
System.out.println(threadname + "正在为您出票,请耐心等待。。。");
for (int i=0; i<this.tikets.length; i++) {
System.out.println("正在打印第 " + (i + 1) + " 张票。");
}
}
}
public static void main(String[] args) throws Exception{
try{
Reader reader = new InputStreamReader(System.in);
BufferedReader buffreader = new BufferedReader(reader);
String input;
System.out.println("请输入出票数量(exit退出):");
while ((input = buffreader.readLine()) != null){
if (input.indexOf("exit") > -1) break;
if (input == "" || input.equals("")){
System.out.println("请输入出票数量(exit退出):");
continue;
}
int tiketsamount = Integer.valueOf(input);
String[] traintikets = new String[tiketsamount];
for (int index=0; index<traintikets.length; index++){
traintikets[index] = "Tiket" + index;
}
SubThread checkout = new SubThread(traintikets);
checkout.setName("出票机");
checkout.start();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
- 实现Runnable接口
实现Runnable接口,重写Runnable的run()方法,创建子类对象,并用子类对象创建Thread线程对象,调用start()方法启动线程。Runnable对象不是线程对象,所以不能直接启动线程。
import java.io.*;
public class ThreadRunnable implements Runnable {
private String[] tikets;
public ThreadRunnable(){
}
public ThreadRunnable(String[] tikets){
this.tikets = tikets;
}
public String[] getTikets(){
return this.tikets;
}
public void setTikets(String[] tikets){
this.tikets = tikets;
}
public void run() {
if (this.tikets == null || this.tikets.length == 0){
System.out.println("没有可出的票据,请输入票据重新启动!");
}else{
String ticketname = Thread.currentThread().getName();
System.out.println(ticketname + "正在为您出票,请耐心等待。。。");
for (int i=0; i<this.tikets.length; i++){
System.out.println("正在打印第 " + (i + 1) + " 张票。" + tikets[i]);
}
}
}
public static void main(String[] args) throws Exception {
try{
var reader = new InputStreamReader(System.in);
var buffreader = new BufferedReader(reader);
String input;
System.out.println("请输入出票数量(exit退出):");
while ((input = buffreader.readLine()) != null) {
if (input.equals("exit")) break;
if(input.equals("")){
System.out.println("请输入出票数量(exit退出):");
continue;
}
int ticketsamount = Integer.valueOf(input);
String[] traintickets = new String[ticketsamount];
for (int index=0; index<traintickets.length; index++) {
traintickets[index] = "Ticket" + (index + 1);
}
var threadrun = new ThreadRunnable(traintickets);
var checkout = new Thread(threadrun,"出票机");
checkout.start();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
- 实现Callable接口
Callable接口需要和Future接口配合使用,Callable对象不能直接用来实例化Thread对象,需要把Callable对象包装为Future对象,然后用Future对象创建线程对象。Callable接口提供call()方法,实现Callable接口要重写该方法作为线程执行体调用,实现线程处理的内容。call()方法可以有返回值,可以抛出异常。call()方法的返回值可以用Future的get()方法获取,get()方法是线程阻塞的,直到获取返回值才执行线程后续操作。
import java.io.*;
import java.util.concurrent.*;
public class ThreadCallable implements Callable<String> {
private String[] tickets;
public ThreadCallable(){
}
public ThreadCallable(String[] tickets){
this.tickets = tickets;
}
public void setTickets(String[] tickets){
this.tickets = tickets;
}
public String[] getTickets(){
return this.tickets;
}
public String call() throws InterruptedException {
if (this.tickets == null || this.tickets.length == 0) {
return "出票失败,没有可用票据!";
}else {
String threadname = Thread.currentThread().getName();
System.out.println(threadname + "正在为您出票,请耐心等待。。。");
for (int i=0; i<this.tickets.length; i++) {
System.out.println("正在打印第 " + (i + 1) + " 张票。" + this.tickets[i]);
Thread.sleep(2000);
}
}
return "打印票据总数 " + this.tickets.length + " 张。";
}
public static void main(String[] args) throws Exception {
try{
var reader = new InputStreamReader(System.in);
var buffreader = new BufferedReader(reader);
String input;
System.out.println("请输入出票数量(exit退出):");
while ((input = buffreader.readLine()) != null) {
if (input.equals("exit")) break;
if(input.equals("")){
System.out.println("请输入出票数量(exit退出):");
continue;
}
int ticketsamount = Integer.valueOf(input);
String[] traintickets = new String[ticketsamount];
for (int index=0; index<traintickets.length; index++) {
traintickets[index] = "Ticket" + (index + 1);
}
var threadcal = new ThreadCallable(traintickets);
FutureTask<String> future = new FutureTask<String>(threadcal);
var checkout = new Thread(future,"出票机");
checkout.start();
String callret = future.get();
System.out.println(callret);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
|