2020.8.5课堂笔记(多线程)

线程的创建方式:
继承Thread类:
实现Runnable接口
实现Callable接口

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);

    }

}

你可能感兴趣的:(笔记)