java中線程的實現方式 繼承Thread類 代碼 : package ThreadImplamen; import static java.lang.Thread.sleep; public class threadImp { public static void main(String[] ar ...
java中線程的實現方式
- 繼承Thread類
代碼 :
package ThreadImplamen;
import static java.lang.Thread.sleep;
public class threadImp {
public static void main(String[] args) {
Thread thread = new Imp1();
thread.start();
int i;
for (i=0;i<5;i++){
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Imp1 extends Thread{
@Override
public void run() {
int i;
for (i=0;i<5;i++){
System.out.println("繼承 Thread類實現的線程!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
測試:
Main!
繼承 Thread類實現的線程!
Main!
繼承 Thread類實現的線程!
Main!
繼承 Thread類實現的線程!
Main!
繼承 Thread類實現的線程!
繼承 Thread類實現的線程!
Main!
- 實現Runnable介面
代碼:
package ThreadImplamen;
import static java.lang.Thread.sleep;
public class ThreadImpl2 {
public static void main(String[] args) {
Runnable run = new RunnableImp();
Thread thread = new Thread(run);
thread.start();
int i;
for (i=0;i<5;i++){
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class RunnableImp implements Runnable{
@Override
public void run() {
int i;
for (i=0;i<5;i++){
System.out.println("實現Runnable介面實現的線程!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
測試:
實現Runnable介面實現的線程!
Main!
Main!
實現Runnable介面實現的線程!
實現Runnable介面實現的線程!
Main!
實現Runnable介面實現的線程!
Main!
Main!
實現Runnable介面實現的線程!
- 實現Callable介面
此方法可接受返回結果
代碼:
package ThreadImplamen;
import static java.lang.Thread.sleep;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javax.script.ScriptException;
public class ThreadImpl3 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable callable = new CallableImp();
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask);
thread.start();
int i;
for (i = 0; i < 5; i++) {
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(futureTask.get());
}
}
class CallableImp implements Callable {
@Override
public Object call() throws Exception{
int i;
for (i = 0; i < 5; i++) {
System.out.println("實現Callable介面實現的線程!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return String.valueOf("這是 你要的返回值");
}
}
測試:
Main!
實現Callable介面實現的線程!
實現Callable介面實現的線程!
Main!
Main!
實現Callable介面實現的線程!
Main!
實現Callable介面實現的線程!
Main!
實現Callable介面實現的線程!
這是 你要的返回值
- 線程池
代碼:
package ThreadImplamen;
import static java.lang.Thread.sleep;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadImpl4 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
int pcount = Runtime.getRuntime().availableProcessors();
//最大線程數控制
int maxthreadNum = 5;
ExecutorService executor = new ThreadPoolExecutor(pcount, maxthreadNum, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 2; i++) {
executor.execute(new test1());
}
int i;
for (i = 0; i < 5; i++) {
System.out.println("Main!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class test1 implements Runnable{
@Override
public void run() {
int i;
for (i = 0; i < 5; i++) {
System.out.println("線上程池內!");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
測試:
線上程池內!
線上程池內!
Main!
線上程池內!
線上程池內!
Main!
線上程池內!
線上程池內!
Main!
線上程池內!
線上程池內!
Main!
線上程池內!
Main!
線上程池內!
個人作品,
如有錯誤,請指出;
如要轉載,請註明出處。
三克油。。