美文网首页
多线程_1_体会线程

多线程_1_体会线程

作者: mm_cuckoo | 来源:发表于2018-03-07 20:33 被阅读19次

进程和线程

进程:

进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。

线程:

在单个程序中同时运行多个线程完成不同的工作。

线程和进程的关系:

通俗的理解,一个进程包含多个线程,也就是说,进程是线程运行的基础,如果没有进程也就无从谈起线程,一个进程可以运行一个线程,也可以运行多个线程。

进程和线程这里不多介绍,主要是为下面线程做铺垫。

线程

下面介绍几种线程的写法

  1. 继承Thread类重写 run 方法,如下

    public class MyThead extends Thread {
        @Override
        public void run() {
            System.out.println("this MyThead run ......");
        }
    }
    
    
    //运行
    MyThead thead = new MyThead();
    thead.start();
    
  2. 实现Runnable接口方法, 如下

    public class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("this MyRunnable run ......");
        }
    }
    
    //运行
    MyRunnable runnable = new MyRunnable();
    Thread thread = new Thread(runnable);
    thread.start();
    
  3. 直接通过Thread 类,重写Thread类中的run 方法, 如下

    Thread thread = new Thread() {
        @Override
        public void run() {
            System.out.println("this Thread run ......");     
        }
    };
    thread.start();
    
  4. 直接通过new Runnable 方法,如下

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("this Runnable run ......");      
    }
});
thread.start();

上面介绍了四种实现线程的方法,不多解释,很简单,就是复习一下。

一些常用的API

  1. Thread.currentThread()
    说明:返回代码段正在被哪个线程调用的信息。

    • Thread.currentThread().getName();
      获取线程名
    • Thread.currentThread().getId();
      获取线程ID
    • Thread.currentThread().getPriority();
      获取线程的优先级
    • Thread.currentThread().getState();
      获取线程的状态
  2. thead.isAlive();
    说明: 判断当前的线程是否处于活动状态

    MyThead thead = new MyThead();
    thead.start();
    if (thead.isAlive()) {
        // is live
    } else {
        // no live
    }
    
  3. Thread.sleep(n);
    说明:在指定的n毫秒内让当前“正在执行的线程”休眠(暂停执行)。

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    

简单介绍几个,如果想要了解更多,自己查询Java API吧!

注:以上及以后的线程相关的内容,都是基于有一定Java 基础的,在写的时候尽量让看的人都能看得懂。如有地方写错了,请及时指出,一起学习,一起进步。

相关文章

  • 多线程_1_体会线程

    进程和线程 进程: 进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。 线程: 在单个程序中同时运行...

  • 多线程(上)

    day24(多线程(上)) 1_多线程(多线程的引入)(了解) 1.什么是线程线程是程序执行的一条路径, 一个进程...

  • 多线程(下)&GUI

    day25(多线程(下)&GUI) 1_多线程(单例设计模式)(掌握) 单例设计模式:保证类在内存中只有一个对象。...

  • iOS 多线程 学习

    今天写一篇关于多线程学习的心得体会。1.先介绍iOS多线程开发的三种方式;2.再介绍多线程开发应该注意的几个点。 ...

  • 多线程查数据库简单案列

    之前一直没有用过多线程,这次,写了一个多线程查询数据库的案列,并且比较一下多线程和单线程直接执行的效率,体会一下多...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

网友评论

      本文标题:多线程_1_体会线程

      本文链接:https://www.haomeiwen.com/subject/mgusfftx.html