JAVA并发之多线程基础(1)

在JAVA中,谈到并发问题确定是谈到多线程相关。能够说这两个之间有着必定的关系。在这里就为你们简单的说下多线程的基础,以后的文章中会继续讲解多线程相关。java

什么是线程

线程是进程内的执行单元。一个进程中含有若干个线程,线程能够看作粒度更小的任务执行单元。进程之间切换,消耗的资源是很大的。经过线程去处理会很好的节省相关资源。多线程

线程状态转化

这上面是针对于线程的状态转化一个简单描述。这上面能够看到blocked的之间的状态只有一条线来回。这块的话关乎到sychronized的底层实现原理,你们能够自行了解下。这里面我就省略了一个Running的一个状态。Running的状态在可运行状态(Runnable)以后。并发

线程实现几种方式

  • 继承Thread类
  • 实现Runnable接口
  • 实现Callable接口

上面是是实现线程的三种方式。在调用线程时候,调用的是其start()方法,而不是调用的run()方法。其中第三种实现方式结合Future能够获取到线程的返回值(同时,在线程池里面运行多线程的话,使用exec.execute()是拿不到线程执行的返回值,使用exec.submit()能够经过上面的Future取得线程执行的返回值)。this

线程方法

1.stop(),这个方法使得当前所运行的线程中止,释放全部的monitor。可是使用这个方法会致使多线程的数据不一致性(假设两个线程执行中去中止,再次操做的时候,线程2有可能抢到本来线程1执行的步骤上)。spa

@Deprecated   //提示过时方法
    public final void stop() {
        //获取SecurityManager进行校验
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            checkAccess();
            if (this != Thread.currentThread()) {
                security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
            }
        }
        if (threadStatus != 0) {
            // 若是线程被挂起,则唤醒线程;不然不执行任何操做
            resume(); 
        }

        // 调用系统方法去中止线程
        stop0(new ThreadDeath());
    }
复制代码

2.interrupt(),这个方法会设置线程中断状态。是一个比较合理使用的一个方法。线程

public void interrupt() {
        if (this != Thread.currentThread())
            checkAccess();

        synchronized (blockerLock) {//加锁控制
            Interruptible b = blocker;
            if (b != null) {
                interrupt0();  // 设置中断位
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }
复制代码

3.interrupted()判断当前线程是否被中断,而且清除当前线程中断状态。code

public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }
复制代码

4.sleep()方法是让线程等待的。在大多数线程等待的方法中都会抛出InterruptedException异常。同时该方法不会释放锁以及所占用的资源。同时它会清除中断标志,因此在catch中再对该线程设置下中断标志。cdn

public static native void sleep(long millis) throws InterruptedException;
复制代码

5.suspend()方法是让当前线程挂起,不会释放锁。blog

@Deprecated
    public final void suspend() {
        checkAccess();
        suspend0();
    }
复制代码

6.resume()方法是让线程继续执行。使用这个的方法就有一个注意点,若是调用这个方法在suspend以前,那么线程就会永久的挂起,这个时候就会永久的占用了资源,可能会致使死锁的发生。继承

@Deprecated
    public final void resume() {
        checkAccess();
        resume0();
    }
复制代码

7.join()的主要做用就是同步,它可使得线程之间的并行执行变为串行执行。

public final synchronized void join(long millis) throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }
复制代码

这里面实际上调用的仍是Object中的wait()方法。

这块也是对于当前使用最多的线程操做方法作了一个小的总结,在下一讲中我会谈及到JUC下面的各个锁操做。

相关文章
相关标签/搜索