多线程

public class Main {
    static int i = 0;
    public static void main(String[] args) {
        //启动线程
        new mThread().start();
        new mThread().start();
        //主线程中循环
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}
class mThread extends Thread {
    int i = 0;
    @Override
    public void run() {
        super.run();
        for (; i < 100; i++) {
            System.out.println(getName() + "->" + i);
        }
    }
}

public class Main {
    static int i = 0;
    public static void main(String[] args) {
        //启动线程
        mThread mThread = new mThread();
        new Thread(mThread).start();
        new Thread(mThread).start();        
        //主线程中循环
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}
class mThread implements Runnable {
    int i = 0;
    @Override
    public void run() {
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}

注意: 上面代码中的Thread 公用的一个Runnable Target 所以变量i只有一份 打印的时候 两个线程公用的是一个变量i,两个线程 不共用一个线程方法就是 创建两个Runnable Target 这样就会创建两个变量i线程和线程之间就没有联系了

public class Main {
    static int i = 0;
    public static void main(String[] args) {
        //启动线程
        new Thread(new mThread()).start();
        new Thread(new mThread()).start();      
        //主线程中循环
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}
class mThread implements Runnable {
    int i = 0;
    @Override
    public void run() {
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Main main = new Main();
        // 创建线程,创建带返回值的Callable线程
        FutureTask<Integer> task = new FutureTask<>((Callable<Integer>) () -> {
            int i = 0;
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
            return i;
        });
        // 启动线程
        new Thread(task).start();
        // 主线程
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
        }
        System.out.println("子线程的返回值为:"+task.get());
    }
}
线程状态图
public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 创建线程,创建带返回值的Callable线程
        FutureTask<Integer> task = new FutureTask<>((Callable<Integer>) () -> {
            int i = 0;
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
            return i;
        });
        // 创建线程空间
        Thread thread = new Thread(task);
        // 主线程
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
            if(i == 20){
                //启动线程
                thread.start();
                //让当前线程等待
                thread.join();
            }
        }
        System.out.println("子线程的返回值为:"+task.get());
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        for(int i = 0;i <=100;i++){
            System.out.println(Thread.currentThread().getName() + "->" + i);
            if(i==20){
                mThread thread = new mThread();
                mThread thread1 = new mThread();
                thread.start();
                thread1.start();
                thread.join();
            }
        }
    }
}

class mThread extends Thread {
    int i = 0;
    @Override
    public void run() {
        super.run();
        for (; i < 100; i++) {
            System.out.println(getName() + "->" + i);
        }
    }
}

小结:如果在主线程中创建两个线程(thread ,thread 1)然后在主线程执行thread的join方法,会让主线程等待thread执行完成后才会获得调度。期间 thread和thread1会共同执行。

public class Main {
    public static void main(String[] args) throws InterruptedException {
        for(int i = 0;i <=100;i++){
            System.out.println(Thread.currentThread().getName() + "->" + i);
            if(i == 20){
                mThread thread = new mThread();
                //设置为守护线程,主线程执行完成后 不需要等待子线程执行完就可以退出虚拟机
                thread.setDaemon(true);
                thread.start();}}}}
class mThread extends Thread {
    int i = 0;
    @Override
    public void run() {
        super.run();
        for (; i < 800; i++) {
            System.out.println(getName() + "->" + i);}}}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i <= 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" + i);
            if (i == 20) {
                mThread thread = new mThread();
                thread.start();
            }
            if(i == 50){
                Thread.sleep(5000);
            }
        }
    }
}
class mThread extends Thread {
    int i = 0;
    @Override
    public void run() {
        super.run();
        for (; i < 1000; i++) {
            try {sleep(1);} catch (InterruptedException e) {}
            System.out.println(getName() + "->" + i);}}}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        mThread thread = new mThread();
        mThread thread1 = new mThread();
        thread.setPriority(Thread.MAX_PRIORITY);
        thread1.setPriority(Thread.MIN_PRIORITY);
        thread.start();
        thread1.start();
    }
}

class mThread extends Thread {
    int i = 0;
    @Override
    public void run() {
        super.run();
        for (; i < 100; i++) {
            try {
                sleep(1);
            } catch (InterruptedException e) {
            }
            System.out.println(getName() + "->" + i);
            // 线程让步
            if (i == 20) {
                yield();
            }
        }
    }
}


先看代码

public class Main {
    public static void main(String[] args) throws InterruptedException {
        mThread thread = new mThread();
        mThread thread1 = new mThread();
        mThread thread2 = new mThread();
        mThread thread3 = new mThread();
        thread.start();
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class mThread extends Thread {
    static int i = 100;
    @Override
    public void run() {
        super.run();
        try {
            sleep(10);
        } catch (InterruptedException e) {
        }
        for (; i > 0;--i) {
            System.out.println(getName() + "->" + i);
        }
    }
}

上面代码正常来说只会出现一个100 可是大多数会出现多个100,这显然是不是我们想要的。

public class Main {
    public static void main(String[] args) throws InterruptedException {
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
        new mThread().start();
    }
}

class mThread extends Thread {
    static Integer i = 1000;
    @Override
    public void run() {
        super.run();
        try {
            sleep(10);
        } catch (InterruptedException e) {
        }
        synchronized(java.util.Calendar.getInstance()){
            for (; i > 0;--i) {
                System.out.println(getName() + "->" + i);
            }
        }
    }
}

同步方法:

BlockingQuaue实现类和父类关系图
线程池工具类类图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文主要讲了java中多线程的使用方法、线程同步、线程数据传递、线程状态及相应的一些线程函数用法、概述等。 首先讲...
    李欣阳阅读 2,503评论 1 15
  • Java多线程学习 [-] 一扩展javalangThread类 二实现javalangRunnable接口 三T...
    影驰阅读 2,994评论 1 18
  • 该文章转自:http://blog.csdn.net/evankaka/article/details/44153...
    加来依蓝阅读 7,381评论 3 87
  • 写在前面的话: 这篇博客是我从这里“转载”的,为什么转载两个字加“”呢?因为这绝不是简单的复制粘贴,我花了五六个小...
    SmartSean阅读 4,792评论 12 45
  • 此刻的小丁内心有些忐忑不安,关于职务变动的事很敏感,一旦出现差错,后果不堪设想,有可能连现有的岗位都保不住。小丁不...
    东风东风阅读 446评论 11 10