Java 多线程的使用

Java 多线程的使用

1.继承Thread,实现多线程

/**
 * @author 黔程似景
 * @description 多线程
 * @date 2022/1/6 21:26
 **/
public class MyThread extends Thread {
    
    public MyThread(String name){
        super(name);//定义线程名
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"----------"+i);
        }
    }

    public static void main(String[] args) {
        //子线程,必须写在主线程前面,必须调用start()启动线程
        Thread thread = new MyThread("liqq");
        thread.start();


        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"----------"+i);
        }
    }
}

2.实现线程接口Runnable

/**
 * @author 黔程似景
 * @description 以接口的方式实现,多线程
 * @date 2022/1/6 22:49
 **/
public class MyRunnable implements Runnable {
    /**
     * 实现多线程
     */
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"----------"+i);
        }
    }
    
    public static void main(String[] args) {
        Runnable target = new MyRunnable();
        Thread thread = new Thread(target);
        thread.start();

        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"----------"+i);
        }
    }
}

3.实现带有返回值的线程Callable

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 黔程似景
 * @description 带有返回值的多线程
 * @date 2022/1/6 23:22
 **/
public class MyCallable implements Callable<String> {
    private int n;

    public MyCallable(int n) {
        this.n = n;
    }
    
    public String call() throws Exception {
        int count = 0;
        for (int i = 1; i <= n; i++) {
//            System.out.println(Thread.currentThread().getName() + "------------------------>" + i);
            count += i;
        }
        return "统计1~" + n + "的和为:" + count;
    }

    public static void main(String[] args) {
        Callable<String> myCall = new MyCallable(10);
        FutureTask<String> myFuture = new FutureTask<>(myCall);
        Thread thread = new Thread(myFuture);
        thread.setName("liqq")
        thread.start();

        Callable<String> call = new MyCallable(100);
        FutureTask<String> future = new FutureTask<>(call);
        Thread thread1 = new Thread(future);
        thread1.start();

        try {
            String s = myFuture.get();
            System.out.println(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        try {
            String s = future.get();
            System.out.println(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
    
}

4.线程常用api

/**
 * 获取当前线程
 */
Thread.currentThread();

/**
 * 设置当前线程名称
 */
Thread.currentThread().setName();

/**
 * 获取当前线程名称
 */
Thread.currentThread().getName();

/**
 * 使线程睡眠----->存在异常
 */
Thread.sleep(Long millis);

/**
 * 设置优先级:
 *		Thread.MAX_PRIORITY(最高)
 *		Thread.NORM_PRIORITY(默认) 
 *		Thread.MIN_PRIORITY(最低) 
 */
final void setPriority(int newPriority);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值