java线程学习(一)

时间:2023-03-09 02:01:01
java线程学习(一)

1.简介

java基础知识部分线程创建的三种方式、线程执行的样例。

代码地址:http://git.oschina.net/blue_phantom/javaj

包位置:package com.bluej.javaj.thread.first;

2.创建线程

继承Thread类、实现Runnale接口、实现Callable接口。

 /**
* 创建线程方式一
* @author xingdongjie
*
*/
class CreateThread1 extends Thread {
@Override
public void run() {
threadHandle();
} private void threadHandle(){
System.out.println("---通过继承Thread:do what you want---");
}
}
/**
* 创建线程方式二
* @author xingdongjie
*
*/
class CreateThread2 implements Runnable {
@Override
public void run() {
threadHandle();
}
private void threadHandle(){
System.out.println("---通过实现Runnable:do what you want---");
}
}
/**
* 线程创建方式三
* @author xingdongjie
*
*/
class CreateThread3 implements Callable<CallableResult>{
private CallableResult result; @Override
public CallableResult call() throws Exception {
result = new CallableResult();
result.setStart(System.currentTimeMillis()); threadHandle(); result.setEnd(System.currentTimeMillis());
result.setUserTime(result.getEnd()-result.getStart());
result.setMessage("--执行成功--"); return result;
} private void threadHandle(){
try {
Thread.sleep(new Random().nextInt(10) * 1000);
} catch (Exception e) {
}
System.out.println("---通过实现Callable:do what you want---");
}
}

创建线程代码示例

第三种方式实现Callable接口,可以返回执行结果,所以这里创建了一个CallableResult类用做执行结果类。

 /**
* callable方式线程执行结果类
* @author xingdongjie
*
*/
class CallableResult implements Serializable{
private static final long serialVersionUID = 1L; private Long start;
private Long end;
private Long userTime;
private String message; public Long getStart() {
return start;
}
public void setStart(Long start) {
this.start = start;
}
public Long getEnd() {
return end;
}
public void setEnd(Long end) {
this.end = end;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
} public Long getUserTime() {
return userTime;
}
public void setUserTime(Long userTime) {
this.userTime = userTime;
}
@Override
public String toString() {
return "CallableResult [start=" + start + ", end=" + end
+ ", userTime=" + userTime + ", message=" + message + "]";
}

CallableResult代码示例

3.执行线程

创建完线程后,启动线程。方式一Thread、方式二Runnale创建的线程后执行方式相同,但是方式三Callable创建的线程执行有些不同。

 /**
* 执行线程方式一
*/
public static void doTread1(){
CreateThread1 thread1 = new CreateThread1();
new Thread(thread1).start();
} /**
* 执行线程方式二
*/
public static void doThread2(){
CreateThread2 thread2 = new CreateThread2();
new Thread(thread2).start();
} /**
* 执行线程方式三
*/
public static void doThread3(){
CreateThread3 thread3 = new CreateThread3();
CallableResult result=null;
//可以通过2种方式执行Callable线程
result =chooseOne(thread3);
//result = chooseTwo(thread3);
System.out.println("---线程执行结果result:"+result); }

线程执行代码示例

Callable线程的执行在这里有两种不同的执行方式,所以用了两个方法,使用时可以二选一。

 /**
* 通过FutureTask方式执行线程
* @param thread3
* @return
*/
public static CallableResult chooseOne(CreateThread3 thread3){
CallableResult result=null;
FutureTask<CallableResult> future = new FutureTask<CallableResult>(thread3);
new Thread(future).start();
try {
result = future.get();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 通过线程池方式执行线程
* @param thread3
* @return
*/
public static CallableResult chooseTwo(CreateThread3 thread3){
CallableResult result=null;
//创建一种简单的线程池
ExecutorService executorService = Executors.newCachedThreadPool();
Future<CallableResult> future = executorService.submit(thread3);
try {
result = future.get();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

执行Callable线程代码示例

4.小结

以上是线程创建及执行的不同方式。

 package com.bluej.javaj.thread.first;

 import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask; /**
* 线程的创建及执行
* @author xingdongjie
*
*/
public class CreateAndDoThread { /**
* @param args
*/
public static void main(String[] args) {
//3种创建线程的执行demo
//doTread1();
//doThread2();
doThread3(); } /**
* 执行线程方式一
*/
public static void doTread1(){
CreateThread1 thread1 = new CreateThread1();
new Thread(thread1).start();
} /**
* 执行线程方式二
*/
public static void doThread2(){
CreateThread2 thread2 = new CreateThread2();
new Thread(thread2).start();
} /**
* 执行线程方式三
*/
public static void doThread3(){
CreateThread3 thread3 = new CreateThread3();
CallableResult result=null;
//可以通过2种方式执行Callable线程
result =chooseOne(thread3);
//result = chooseTwo(thread3);
System.out.println("---线程执行结果result:"+result); }
/**
* 通过FutureTask方式执行线程
* @param thread3
* @return
*/
public static CallableResult chooseOne(CreateThread3 thread3){
CallableResult result=null;
FutureTask<CallableResult> future = new FutureTask<CallableResult>(thread3);
new Thread(future).start();
try {
result = future.get();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 通过线程池方式执行线程
* @param thread3
* @return
*/
public static CallableResult chooseTwo(CreateThread3 thread3){
CallableResult result=null;
//创建一种简单的线程池
ExecutorService executorService = Executors.newCachedThreadPool();
Future<CallableResult> future = executorService.submit(thread3);
try {
result = future.get();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
} /**
* 创建线程方式一
* @author xingdongjie
*
*/
class CreateThread1 extends Thread {
@Override
public void run() {
threadHandle();
} private void threadHandle(){
System.out.println("---通过继承Thread:do what you want---");
}
}
/**
* 创建线程方式二
* @author xingdongjie
*
*/
class CreateThread2 implements Runnable {
@Override
public void run() {
threadHandle();
}
private void threadHandle(){
System.out.println("---通过实现Runnable:do what you want---");
}
}
/**
* 线程创建方式三
* @author xingdongjie
*
*/
class CreateThread3 implements Callable<CallableResult>{
private CallableResult result; @Override
public CallableResult call() throws Exception {
result = new CallableResult();
result.setStart(System.currentTimeMillis()); threadHandle(); result.setEnd(System.currentTimeMillis());
result.setUserTime(result.getEnd()-result.getStart());
result.setMessage("--执行成功--"); return result;
} private void threadHandle(){
try {
Thread.sleep(new Random().nextInt(10) * 1000);
} catch (Exception e) {
}
System.out.println("---通过实现Callable:do what you want---");
}
}
/**
* callable方式线程执行结果类
* @author xingdongjie
*
*/
class CallableResult implements Serializable{
private static final long serialVersionUID = 1L; private Long start;
private Long end;
private Long userTime;
private String message; public Long getStart() {
return start;
}
public void setStart(Long start) {
this.start = start;
}
public Long getEnd() {
return end;
}
public void setEnd(Long end) {
this.end = end;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
} public Long getUserTime() {
return userTime;
}
public void setUserTime(Long userTime) {
this.userTime = userTime;
}
@Override
public String toString() {
return "CallableResult [start=" + start + ", end=" + end
+ ", userTime=" + userTime + ", message=" + message + "]";
} }

所有代码示例