package rejava3.线程;
import javax.swing.*;
import javax.swing.plaf.nimbus.State;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
class MyThread1 extends Thread{
boolean flag=true;
public String people;
public MyThread1(String people){
this.people=people;
}
public void run(){
Date tim=null;
for(int o=0;o<10;o++){
try {
tim=new Date(System.currentTimeMillis());
Thread.sleep(1000);
System.out.println("系统时间"+new SimpleDateFormat("HH:mm:ss").format(tim));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void autoStop(){
this.flag=false;
}
}
class MyThread2 implements Runnable {
String name;
public MyThread2(String name) {
this.name = name;
}
public void run() {
for (int i = 10; i > 0; i--) {
try {
Thread.sleep(1000);
System.out.println(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestThread {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyThread1 threadA1 = new MyThread1("清清");
threadA1.start();
new MyThread1("楚楚").start();
MyThread2 threadB1 = new MyThread2("清清");
MyThread2 threadB2 = new MyThread2("楚楚");
Thread a1 = new Thread(new MyThread2("清清"));
threadA1.setPriority(Thread.MAX_PRIORITY);
a1.setPriority(1);
System.out.println(threadA1.getPriority());
a1.start();
try {
a1.join();
threadA1.yield();
Thread.State state=a1.getState();
threadA1.setDaemon(true);
} catch (InterruptedException ew) {
System.out.println("中断咯");
}
new Thread(threadB2,"名字").start();
new Thread( ()->System.out.println("") ).start();
System.out.println("=========================================================================");
MyThread3 threadC1 = new MyThread3();
MyThread3 threadC2 = new MyThread3();
ExecutorService ser = Executors.newFixedThreadPool(2);
Future<Boolean> r1 = ser.submit(threadC1);
Future<Boolean> r2 = ser.submit(threadC2);
Boolean rb1 = r1.get();
Boolean rb2 = r2.get();
System.out.println(rb1 + "," + rb2);
}
}
class MyThread3 implements Callable {
@Override
public Boolean call(){
System.out.println(Thread.currentThread().getName());
return true;
}
}