Java多线程学习笔记(自用)

Java多线程

Thread类中的主要方法
  1. start():开启线程
  2. run():继承重写run()
  3. currentThread():返回当前线程
  4. getName():返回线程名
  5. setName():设置线程名
  6. yield():释放cpu执行权
  7. b.join():a线程阻塞并等待b线程执行完成
  8. stop():已过时,强制结束线程
  9. sleep():睡眠ms
  10. isAlive():是否存活
创建方式,jdk5.0前两种,jdk5.0后两种
  1. 继承Thread类
  2. 实现Runnable接口
  3. 实现Callable接口
  4. 线程池
1.继承Thread的方法
/**
 * @author Microdust
 * @create 2020/11/24-19:40
 */
class MyThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() +
                        ":" + i);
            }
        }
    }
}
public class MyThreadTest1 {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
	t1.setName("thread1");
        t1.start();
    }

}
2.实现Runnable接口的方法
class MyThread2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() +
                        ":" + i);
            }
        }
    }
}
public class MyThreadTest2 {
    public static void main(String[] args) {
        MyThread2 m1 = new MyThread2();
        Thread t1 = new Thread(m1);
        t1.setName("thread1");
        t1.start();
    }

}
3.实现Callable接口的方法
class MyThread3 implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                sum += i;
                System.out.println(Thread.currentThread().getName() +
                        ":" + i);
            }
        }
        return sum;
    }
}
public class MyThreadTest3 {
    public static void main(String[] args) {
        FutureTask futureTask = new FutureTask(new MyThread3());//Runnable
        Thread t1 = new Thread(futureTask);
        t1.setName("thread1");
        t1.start();

        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}
4.使用线程池创建线程
class MyThread4 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() +
                        ":" + i);
            }
        }
    }
}
class MyThread5 implements Callable{
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                sum += i;
                System.out.println(Thread.currentThread().getName() +
                        ":" + i);
            }
        }
        return sum;
    }
}
public class MyThreadTest4 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);
//        ThreadPoolExecutor service1 = (ThreadPoolExecutor)service;
//        service1.setCorePoolSize(10);
//        service1.setKeepAliveTime(...);
        service.execute(new MyThread4());
        Future future = service.submit(new MyThread5());//返回Future类对象
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        service.shutdown();
    }


}
线程同步方法:
  1. Synchronized (同步代码块,同步方法)同步监视器多个线程共用一个(共享数据时)
  2. Lock接口的实现类ReentrantLock对象的方法,lock.lock()、lock.unlock()
线程通信方法:
  1. wait() 阻塞
  2. notify()/notifyAll() 唤醒
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值