线程到底是怎么去用的,用那些用途?

#线程
1.并发并行 
  案例一,假设有三个学生辅导作业,帮每一个学生完成作业是一个任务
       顺序执行:  老师A-B-C一次给三个学生 假设通过顺序来执行  后面的学生都会在哪里等我们的老师那么就会造成一个时间上的浪费
       并发:老师分别给A学生讲完思路再给B学生去讲解思路后面再给C学生去讲解思路很快就可以完成这三个任务,与顺序执行不同的是老师的时间缩短了但是学生的,但是在讲完思路之后老师完全是空着的,效率十分的低
       并行:直接让三个老师来给我们的三个学生ABC来讲题也能很快的完成,但是成本较高
  案例二,你吃饭吃到一半,来了电话你一直等吃完再去接这个电话说明你不支持并发也不支持并行
        并发:你先去接电话再来吃饭
        并行:边吃边接电话
  案例三:
        并发(一个单行道最多允许8辆汽车通过  突然一下子来了9辆汽车就可能出现安全问题)   
        并行:(现在这个车道变成了8个车道,一辆汽车一个车道,但是现在的代价很大,随着车道的增加费用也会增大)
        高并发(在节假日汽车一下子变成几十万个这就是高并发;或者高并发双十一大家都去抢一个东西如果你的并发处理不好就会出现问题)
专业术语:
      1.并发:不同的代码块交替执行 ()
      2.并行:不同的代码块同时执行
      **  并发(一段时间内通过的人数(单位))和并行(一瞬间通过的人数)同时存在
 ![](线程进程的理解_files/1.jpg)
      
```java

```

#进程  (消耗我们的cpu空间)
进程是给我们的线程一个空间真正干活的是线程(进程开启之后进程中所有的线程都具有了cpu的执行资格,但不一定具有cpu的执行权) 
 (正在运行的程序qq,excel,微信);![](线程进程的理解_files/2.jpg)
#线程
![](线程进程的理解_files/3.jpg)给每一个聊天窗口都开辟了一个新的通道
对同一个进程中cpu给我们的程序开辟了多个通道
JDK使用的就是一个抢占调度方案:(我们的JAVA中的线程的执行是随机的)
cpu所有线程的掌控者
```java
package excirse;

public class ThreadDemo {
    //程序主入口

    /**
     * 主函数就是一个主线程
     * jvm告诉我们的cpu来获取一个资源
     * @param args
     */
    public static void main(String[] args) {
        //线程
        //1.如何获取当前正在运行的线程
        //Thread thread = new Thread();
        System.out.println(Thread.currentThread().getName()+"线程正在运行");//获取当前正在运行的线程的名称
        /**
         * main线程正在运行 打印结果
         * 主线程的执行请求是有我们的jvm自动发送给我们的cpu的,(我们将来自己创建的线程需要我们的手动去启动(争取cpu的执行权)
         */
    }
}
//第二种常见线程的方式
package excirse;

import excirse.empty.printer;

public class MyThread2 {
    public static void main(String[] args) {
        //1.说干匿名内部类来创建一个线程
        printer pt = new printer();
        Thread thread = new Thread()
        {
        @Override
            public void run()
        {
           // super.run();
            for (; ;) {
              //  System.out.println(i);
                pt.print1();
                try {
                    Thread.sleep(1000);//单位是毫秒
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        };
        thread.start();

        Thread tr1 = new Thread(() -> {
            for (; ; ) {
                //  System.out.println(i);
                pt.print2();
                try {
                    Thread.sleep(1000);//单位是毫秒
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        tr1.start();
    };

}

//创建线程的第三种方式

```
###线程的几个题目
   1.通过这个打印机对我们的数据进行轮流打印
   ```java
   package excirse.print;

public class printDemo {
    private  static int num=1;
    //第一个打印欢迎光临
    public void printA()
    {
        synchronized (this) {//这里的this只有一个因此钥匙的也只会有一个
            //其实在这个地方不管是通过哪一种方式都轮流打印
            if (num != 1) {
                //如果不是这个的话就进入等待池
                try {
                    //System.out.println(Thread.currentThread().getName()+"我要去了");
                    this.wait();//他会在这个地方停下来
                  // System.out.println("开始了我"+Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName()+"欢迎光临");
            num=2;
            this.notify();//每一次唤醒一个进程来打印  唤醒也不代表这个进程就可以马上执行
        }
    }
    //打印welcome
    public void printB()
    {
        synchronized (this) {
            if (num != 2) {
             //  System.out.println(Thread.currentThread().getName()+"我要去了");
                try {
                    this.wait();
                 // System.out.println("开始了我"+Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName()+"welcome");
            num=1;
            this.notify();
        }
    }
}

```
2.使用join来完成我们线程的先后
```java
package excirse.Demo1;

/**
 * abc设置一下先后顺序
 */
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread("a县城") {
            @Override
            public void run() {
                //
               // thread1.join();
                for (int i=0;i<100;i++) {
                    System.out.println("线程a开始了");
                }
            }
        };
        Thread thread1 = new Thread("b县城") {
            @Override
            public void run() {
                //
//                try {
//                    thread.join();
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
                synchronized (ThreadDemo.class) {
                    for (int i=0;i<10;i++) {
                        System.out.println("线程b开始了");
                    }
                }
            }
        };
        Thread thread2 = new Thread("c县城") {
            @Override
            public void run() {
                //
//                try {
//                    thread1.join();//我的理解就是这个线程回到了线程池里面才行
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
                synchronized (ThreadDemo.class) {
                    for (int i=0;i<10;i++) {
                        System.out.println("线程c开始了");
                    }
                }
            }
        };
        /**
         * 在这个地方添加代码就是当我们的运行完后才会运行我们的其他的代码
         */
        thread1.start();
        thread1.join();
        thread.start();
        thread.join();
        thread2.start();
        thread2.join();
    }
}

```
3.使用一个线程完成一个顺序打印的过程
```java
package excirse.Demo1;
// 写两个线程,一个线程打印1~ 52,另一个线程打印A~Z,打印顺序是12A34B…5152Z
public class DemoThread {
    public static void main(String[] args) {
        printTest pt = new printTest();
        //两个线程都已经创建ok啦
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int  i=0;i<10;i++) {
                    pt.printA();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<10;i++) {
                    pt.printB();
                }
            }
        }).start();

    }
}
class printTest{
    //添加一个判断语句
    private int a=1;
    private int b=1;
    private int flog=1;
    //打印12给他们上同一个锁
    public synchronized void printA()
    {
           while (flog!=1)
           {
               try {
                   this.wait();//开始等待这个
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
            System.out.print(a+++""+a++);
           flog=2;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        notify();
    }
    //打印a  说白了这个就是一个解救自己的过程不过这个过程需要别人去完成自己是无法完成的
    public   synchronized  void printB()
    {
        while (flog!=2)
        {
            try {
                this.wait();//开始等待这个
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        flog=1;
            System.out.print((char) (64+b));
            b++;

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

      notify();
    }
}
```

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值