2020.8.5クラスノート(マルチスレッド)
29172 ワード
スレッドの作成方法:Threadクラスの継承:Runnableインタフェース実装Callableインタフェース実装
TestMain:
TestThread:
TestRunnable:
TestTicket:
TestMethod:
TestCallable:
TestMain:
public class TestMain {
public static void main(String[] args) {
Thread t=new Thread(" ");
Thread th=Thread.currentThread();
th.setName(" ");
System.out.println(th.getName());
System.out.println(t.getName());
}
}
TestThread:
package cn.kgc.kb09;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/5 14:19
* @Description: Thread
**/
public class TestThread extends Thread {
@Override
public void run() {
for (int i = 1; i <=100 ; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
System.out.println(" , ");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
/*TestThread t1=new TestThread();
t1.setName(" 1");
TestThread t2 = new TestThread();
t2.setName(" 2");
t1.start();
t2.start();*/
TestThread t=new TestThread();// ,
t.setName(" ");
Thread th1=new Thread(t);
th1.setName(" 1");
Thread th2=new Thread(t);
th2.setName(" 2");
th1.start();
th2.start();
}
}
// An highlighted block
var foo = 'bar';
TestRunnable:
package cn.kgc.kb09;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/5 14:41
* @Description: Runnable
**/
public class TestRunnable implements Runnable {// ,
@Override
public void run() {
for (int i = 1; i <=100 ; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
System.out.println(" ");
Thread.yield();//
}
}
public static void main(String[] args) throws InterruptedException {
TestRunnable r=new TestRunnable();
System.out.println(" ");
Thread t1 = new Thread(r," 1");// init
Thread t2 = new Thread(r," 2");
System.out.println(" 1");
t1.start();
//Thread.sleep(100);
System.out.println(" 2");
t2.start();
System.out.println(t2.isAlive());
}
}
TestTicket:
package cn.kgc.kb09;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/5 16:27
* @Description:
**/
public class TestTicket implements Runnable {
int ticket = 1000;
int sold = 0;
@Override
public void run() {
while (ticket > 0) {
synchronized (this) {
ticket--;
sold++;
if (ticket < 0) return;
System.out.println(Thread.currentThread().getName() + " " + sold + " , " + ticket);
}
}
}
public static void main(String[] args) {
TestTicket t = new TestTicket();
Thread t1 = new Thread(t, "123306");
Thread t2 = new Thread(t, " ");
t2.start();
t1.start();
}
}
TestMethod:
package cn.kgc.kb09;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/5 15:19
* @Description:
**/
public class TestMethod {
public static void main(String[] args) throws InterruptedException {
TestRunnable r=new TestRunnable();
Thread t1=new Thread(r," 1");
System.out.println(" 1 ");
System.out.println(" 2 ");
Thread t2=new Thread(r," 2");
t1.join();
System.out.println(" join , t1 ");
// t1.setPriority(10);
// t2.setPriority(1);
t1.start();
t2.start();
/*for(int i=0;i<5;i++){
if(i==2){
t1.join();
}
System.out.println(i+1+" ");
}*/
}
}
TestCallable:
package cn.kgc.kb09;
import java.util.concurrent.*;
/**
* @Author: ChaoKeAiMuZhi
* @Date: 2020/8/5 17:25
* @Description:
**/
public class TestCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+":"+(i+1));
}
return 3;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t=new TestCallable();
FutureTask<Integer> f=new FutureTask<Integer>(t);
Thread th=new Thread(f);
th.start();
System.out.println(f.get());
ExecutorService single = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newFixedThreadPool(3);
}
}