[java] Thread类详解

Unreal 5 MetaHuman插件全流程:从安装到角色动画生成 本文详细介绍了Unreal 5中MetaHuman插件的全流程使用指南,从安装配置到角色动画生成。通过Quixel Bridge下载预设角色,利用面部捕捉技术赋予数字人模型生命力,并提供材质编辑、性能优化等进阶技巧,帮助用户快速创建照片级真实的虚拟角色。 阅读详情

 

Table of Contents

概述

Thread类中的重要成员变量

Thread类中的方法分析

构造方法

start方法

run方法

问题:Thread直接调用run()和start()方法的区别?

sleep方法

yield方法

interrupt方法

join方法

getState方法

holdsLock方法判断线程是否持有锁对象

获取当前线程

设置线程优先级

设置和获取线程名字

守护线程

获取一个线程的栈轨迹

问题:为什么wait和notify方法不在Thread类里定义?

总结


本篇博客基于jdk1.8

概述

Thread的一个实例,即是一个程序中执行的一个线程,java虚拟机允许一个应用程序同时有多个并发的线程运行。

每一个线程都有一个优先级。优先级高的线程比优先级低的线程优先执行。每一个线程也都可能被标记为一个守护线程。当代码执行到某一个线程去创建另一个线程时,新创建的线程的优先级会初始化成和创建它的线程一样。并且当且仅当创建线程为守护线程时,被创建线程才会被设为守护线程。

当Java虚拟机启动时,通常会有一个单独的非守护进程线程(通常这个非守护线程会去调用一个指定的类的main方法)。Java虚拟机器继续执行线程,直到以下任一一种情况发生:

Runtime类的exit方法被调用并且security manager(安全管理器)允许退出操作操作发生。

所有的非守护线程全部都已经终结,要么通过从调用返回到运行方法或由抛出超出运行的异常方法。

创建一个新的执行线程有两种方法:

声明一个Thread类的子类。这个子类应当去重写Thread类里的run方法。然后子类的实例就可以分配和启动。例如,计算大于指定值的素数的线程可以写成如下:

class PrimeThread extends Thread {
          long minPrime;
          PrimeThread(long minPrime) {
              this.minPrime = minPrime;
          }
 
          public void run() {
              // compute primes larger than minPrime
              ...
          }
}

下面的代码就可以创建一个线程并启动它:

PrimeThread p = new PrimeThread(143);
p.start();

创建线程的另一种方法是声明一个类实现Runable接口,这个类需要实现run方法,然后这个类的实例就可以在创建线程时被当做参数传递进去,并且启动,同一个例子的另一种样式如下:

class PrimeRun implements Runnable {
          long minPrime;
          PrimeRun(long minPrime) {
              this.minPrime = minPrime;
          }
 
          public void run() {
              // compute primes larger than minPrime
              ...
          }
}

然后,下面的代码就可以创建一个线程并启动它:

PrimeRun p = new PrimeRun(143);
new Thread(p).start();

每个线程都有用于标识目的的名称。多个线程可能具有相同的名称。如果创建一个线程时未指定名称,则会为其生成一个新名称。

除非另有说明,否则,向Thread类中的构造函数或方法传递null参数将导致抛出NullPointerException。

另外一点特别有意思的是,Thread类本身就是实现了Runnable接口的一个类。

Thread类中的重要成员变量

    private volatile String name;   //线程的名字
    private int priority; //线程优先级
    /* Whether or not to single_step this thread. */
    private boolean     single_step;

    /* Whether or not the thread is a daemon thread. */
    private boolean     daemon = false; //是否是守护线程

    /* What will be run. */
    private Runnable target; //将会被执行的Runnable.

    /* The group of this thread */
    private ThreadGroup group;  //这个线程的组

    /* The context ClassLoader for this thread */
    private ClassLoader contextClassLoader; //这个线程的上下文

    /* The inherited AccessControlContext of this thread */
    private AccessControlContext inheritedAccessControlContext; //继承的请求控制

    /* For autonumbering anonymous threads. */
    private static int threadInitNumber;  //默认线程的自动编号
   
    /* ThreadLocal values pertaining to this thread. This map is maintained
     * by the ThreadLocal class. */
    ThreadLocal.ThreadLocalMap threadLocals = null; //当前线程附属的ThreadLocal,而ThreadLocalMap会被ThreadLocal维护)

    /*
     * InheritableThreadLocal values pertaining to this thread. This map is
     * maintained by the InheritableThreadLocal class.
     */
    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
  // 主要作用:为子线程提供从父线程那里继承的值
  //在创建子线程时,子线程会接收所有可继承的线程局部变量的初始值,以获得父线程所具有的值
  // 创建一个线程时如果保存了所有 InheritableThreadLocal 对象的值,那么这些值也将自动传递给子线程
  //如果一个子线程调用 InheritableThreadLocal 的 get() ,那么它将与它的父线程看到同一个对象

    /*
     * The requested stack size for this thread, or 0 if the creator did
     * not specify a stack size.  It is up to the VM to do whatever it
     * likes with this number; some VMs will ignore it.
     */
    private long stackSize; //该线程请求的堆栈大小 默认一般都是忽略

    /*
     * Thread ID
     */
    private long tid;  // 每个线程都有专属ID,但名字可能重复

    /* For generating thread ID */
    private static long threadSeqNumber;   //用来生成thread ID

    /* Java thread status for tools,
     * initialized to indicate thread 'not yet started'
     */

    private volatile int threadStatus = 0;  //标识线程状态,默认是线程未启动

    /* The object in which this thread is blocked in an interruptible I/O
     * operation, if any.  The blocker's interrupt method should be invoked
     * after setting this thread's interrupt status.
     */
    private volatile Interruptible blocker;  //阻塞器锁,主要用于处理阻塞情况 

    /**
     * The minimum priority that a thread can have.
     */
    public final static int MIN_PRIORITY = 1;//线程的优先级中最小的

   /**
     * The default priority that is assigned to a thread.
     */
    public final static int NORM_PRIORITY = 5; //线程的优先级中第二的同时也是默认的优先级

    /**
     * The maximum priority that a thread can have.
     */
    public final static int MAX_PRIORITY = 10; //最高的优先级

Thread类中的方法分析

构造方法

据完全统计,Thread类里一共提供了九个构造方法...其中有一个是非public的方法。

但是这九个构造方法事实上最后都是调用了一个叫做init的方法,该方法定义如下:

/**
     * Initializes a Thread.
     *
     * @param g the Thread group
     * @param target the object whose run() method gets called
     * @param name the name of the new Thread
     * @param stackSize the desired stack size for the new thread, or
     *        zero to indicate that this parameter is to be ignored.
     * @param acc the AccessControlContext to inherit, or
     *            AccessController.getContext() if null
     * @param inheritThreadLocals if {@code true}, inherit initial values for
     *            inheritable thread-locals from the constructing thread
     */
    private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;

        Thread parent = currentThread();
        SecurityManager security = System.getSecurityManager();
        if (g == null) {
            /* Determine if it's an applet or not */

            /* If there is a security manager, ask the security manager
               what to do. */
            if (security != null) {
                g = security.getThreadGroup();
            }

            /* If the security doesn't have a strong opinion of the matter
               use the parent thread group. */
            if (g == null) {
                g = parent.getThreadGroup();
            }
        }

        /* checkAccess regardless of whether or not threadgroup is
           explicitly passed in. */
        g.checkAccess();

        /*
         * Do we have the required permissions?
         */
        if (security != null) {
            if (isCCLOverridden(getClass())) {
                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
            }
        }

        g.addUnstarted();

        this.group = g;
        this.daemon = parent.isDaemon();
        this.priority = parent.getPriority();
        if (security == null || isCCLOverridden(parent.getClass()))
            this.contextClassLoader = parent.getContextClassLoader();
        else
            this.contextClassLoader = parent.contextClassLoader;
        this.inheritedAccessControlContext =
                acc != null ? acc : AccessController.getContext();
        this.target = target;
        setPriority(priority);
        if (inheritThreadLocals && parent.inheritableThreadLocals != null)
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;

        /* Set thread ID */
        tid = nextThreadID();
    }

该方法一共六个参数:

1.ThreadGroup 线程组

2.Runnable  target 一个Runnable对象,即调用run方法的对象

3.String name 新线程的名字

4.long stackSize 新线程分配所需堆栈的大小

5.AccessControlContext acc 权限控制

6.boolean inheritThreadLocals 是否继承ThreadLocals, 这个参数总是true。

方法的实现大致总结起来就是,给变量赋初值,大部分的初值都是通过该线程的父线程赋值,获取父线程对象的代码为:

Thread parent = currentThread();

而currentThread方法的实现是一个本地方法。

start方法

start方法的作用就是让该线程开始执行,java虚拟机会调用这个线程的run方法。

执行start方法的结果是两个线程会并发的运行,当前线程即执行start方法的线程,以及另一个调用run方法的线程。

启动一个线程两次永远是非法的,会抛出IllegalThreadStateException。代码如下:

 public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

可以看到上来会验证线程的状态,如果线程已启动则抛异常。核心实现代码为方法start0,是一个本地方法,在这个本地方法的实现当中,虚拟机会去调用该线程的run方法,在java代码中并看不出这一点。

这就是我们可以通过start方法启动线程,并且run方法会执行的原因所在。

run方法

Thread类实现了Runable接口,自然也就实现了run方法,代码如下

  @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

target定义如下:

    /* What will be run. */
    private Runnable target;

target就是Runnable的一个实例,是Thread类中执行run的对象。注意:这里的代码同时说明了我们new一个Thread的时候问什么要传入一个Runable的对象,因为事实上后来这个Thread启动调用run方法的时候,就是调用的这个Runable的run方法。

问题:Thread直接调用run()和start()方法的区别?

 run()方法: 在本线程内调用该Runnable对象的run()方法,可以重复多次调用;

 start()方法:启动一个全新的线程,然后Java虚拟机会调用该Runnable对象,即new一个Thread的时候传入的Runable对象,的run()方法,不能多次启动一个线程。

调用run()方法的时候,只会是在原来的线程中调用,没有新的线程启动,start()方法才会启动新线程。

sleep方法

有两个重载的sleep方法。其中一个代码如下:

 public static void sleep(long millis, int nanos)
    throws InterruptedException {
        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
            millis++;
        }

        sleep(millis);
    }

在调用另一个sleep方法之前,就是做了一些range check,即范围检查,然后就直接调用了另一个native的sleep方法。

yield方法

代码如下

 public static native void yield();

作用是使当前线程从执行状态(运行状态)变为可执行态(就绪状态)。也是一个native方法。

interrupt方法

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

        synchronized (blockerLock) {
            Interruptible b = blocker;
            if (b != null) {
                interrupt0();           // Just to set the interrupt flag
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }

实现也是调用了一个interrupt0的本地方法,有一句关键的注释:只是去set了interrupt flag,即不会真正的去停止线程,关于这一部分的内容,可以参考我的另一篇博客:停止线程。

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;
            }
        }
    }

join的作用就是使得当前线程阻塞,一直到调用join方法的线程对象执行完run方法中的任务且销毁之后才继续执行后面的代码。

从上面的代码实现来看,join方法是通过和wait方法配合实现的。

getState方法

该方法是返回线程的状态,它的实现很有意思,如下:

 public State getState() {
        // get current thread state
        return sun.misc.VM.toThreadState(threadStatus);
    }
public static State toThreadState(int var0) {
        if ((var0 & 4) != 0) {
            return State.RUNNABLE;
        } else if ((var0 & 1024) != 0) {
            return State.BLOCKED;
        } else if ((var0 & 16) != 0) {
            return State.WAITING;
        } else if ((var0 & 32) != 0) {
            return State.TIMED_WAITING;
        } else if ((var0 & 2) != 0) {
            return State.TERMINATED;
        } else {
            return (var0 & 1) == 0 ? State.NEW : State.RUNNABLE;
        }
    }

toThreadState方法的实现就是根据线程状态的数字,返回对应的状态而已,它们只是有一个mapping的关系。State的定义则是一个枚举,如下:

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

可以看到java的线程一共是有六种状态的。

holdsLock方法判断线程是否持有锁对象

   /**
     * Returns <tt>true</tt> if and only if the current thread holds the
     * monitor lock on the specified object.
     *
     * <p>This method is designed to allow a program to assert that
     * the current thread already holds a specified lock:
     * <pre>
     *     assert Thread.holdsLock(obj);
     * </pre>
     *
     * @param  obj the object on which to test lock ownership
     * @throws NullPointerException if obj is <tt>null</tt>
     * @return <tt>true</tt> if the current thread holds the monitor lock on
     *         the specified object.
     * @since 1.4
     */
    public static native boolean holdsLock(Object obj);

获取当前线程

 /**
     * Returns a reference to the currently executing thread object.
     *
     * @return  the currently executing thread.
     */
    public static native Thread currentThread();

该方法的作用是返回当前执行这行代码的执行线程。

设置线程优先级

public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
    }

该方法的作用是设置线程的优先级,示例代码如下:

ThreadB b = new ThreadB();
b.setPriority(Thread.NORM_PRIORITY + 3);

当然我们也可以获取一个线程的优先级

  public final int getPriority() {
        return priority;
    }

设置和获取线程名字

获取名字

  /**
     * Returns this thread's name.
     *
     * @return  this thread's name.
     * @see     #setName(String)
     */
    public final String getName() {
        return name;
    }

设置一个线程的名字,如果不设置的话,线程将会有一个默认的名字,我们在dubug代码的时候常常会看到以0,1结尾的线程,就是默认的线程名字

public final synchronized void setName(String name) {
        checkAccess();
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;
        if (threadStatus != 0) {
            setNativeName(name);
        }
    }

守护线程

我们可以把一个线程设为守护线程

   public final void setDaemon(boolean on) {
        checkAccess();
        if (isAlive()) {
            throw new IllegalThreadStateException();
        }
        daemon = on;
    }

也可以判断一个线程是否是守护线程

  /**
     * Tests if this thread is a daemon thread.
     *
     * @return  <code>true</code> if this thread is a daemon thread;
     *          <code>false</code> otherwise.
     * @see     #setDaemon(boolean)
     */
    public final boolean isDaemon() {
        return daemon;
    }

获取一个线程的栈轨迹

我们可以获取当前线程的调用栈轨迹,下面的方法将返回表示此线程的堆栈转储的堆栈跟踪元素数组

 public StackTraceElement[] getStackTrace() {
        if (this != Thread.currentThread()) {
            // check for getStackTrace permission
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                security.checkPermission(
                    SecurityConstants.GET_STACK_TRACE_PERMISSION);
            }
            // optimization so we do not call into the vm for threads that
            // have not yet started or have terminated
            if (!isAlive()) {
                return EMPTY_STACK_TRACE;
            }
            StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
            StackTraceElement[] stackTrace = stackTraceArray[0];
            // a thread that was alive during the previous isAlive call may have
            // since terminated, therefore not having a stacktrace.
            if (stackTrace == null) {
                stackTrace = EMPTY_STACK_TRACE;
            }
            return stackTrace;
        } else {
            // Don't need JVM help for current thread
            return (new Exception()).getStackTrace();
        }
    }

我们可以这样使用这个方法:

StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();

通过返回的stackTraceElement 数组,就可以查看到调用栈的信息。

问题:为什么wait和notify方法不在Thread类里定义?

需要说明为什么把这些方法放在Object类里是有意义的,还有不把它放在Thread类里的原因。一个很明显的原因是JAVA提供的锁是对象级的而不是线程级的,每个对象都有锁,通过线程获得。如果线程需要等待某些锁那么调用对象中的wait()方法就有意义了。如果wait()方法定义在Thread类中,线程正在等待的是哪个锁就不明显了。简单的说,由于wait,notify和notifyAll都是锁级别的操作,所以把他们定义在Object类中因为锁属于对象

总结

经过上面的分析,可以发现事实上Thread类因为设计到线程的操作,因此大量的还是依靠了native方法实现。java核心逻辑并不多。

【Flink】Flink 消费一段时间1万多条数据就卡死 我们有个现场环境,发现了奇怪的现象,就是每次启动任务后,消费1万多条数据后就会卡死,然后整个流不消费,也不往下输出,看起来卡死了一样。缺图这里面出现的网闸原因导致的,但是经过我们写了一个简单的kafka消费程序,然后在出问题的节点上消费数据,实际排查,发现不是这个问题。这个问题,发现也不是,这段时间没有进行过kafka数据清理。里面的问题。因为这个问题,我们后面的代码刻意规范了,不这样操作。后来是在没办法了,然后我们dump了线程,发现有如下代码。 阅读详情

相关推荐

5个必知的脑网络图论指标:从聚类系数到小世界特性(附BCT工具包使用指南)

本文深度解析脑网络图论分析中五个核心指标:聚类系数、特征路径长度、全局效率、局部效率及节点效率,并详解其计算与解读。结合Brain Connectivity Toolbox工具包,提供从实战计算、常见报错解决到小世界特性统计检验的完整指南,帮助研究者跨越理论与实操鸿沟,让连接矩阵数据真正服务于神经科学研究。

blue42的博客 936

java thread exit方法_实例分析Java终止线程和stop()方法

Java终止线程实例和stop()方法源码阅读了解线程概念线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。线程特点拥有状态,表示线程的状态,同一时刻中,JVM中的某个线程只有一种状态;·NEW尚未启动的线程(程序运行开始至今一次未启动的线程)·RUNNABLE可运行的线程,正在JVM中运行,但它可能在等待其他资源,如CPU。·BLOCKED阻塞的线程,等待某个锁允许它...

weixin_34393451的博客 3416

GMSL2 协议与技术 第一部分

默认情况下,自动的链路配置功能是使能的(AUTO_LINK = 1),这表示器件将试图锁定在single link模式,它将自动监测是否PHY-A或者PHY-B已经连接到了远端的器件,并且使能这个已连接的PHY,如果PHY-A和PHY-B同时连接到了远端的器件,它将使用首先探测到的连接进入single link 模式。另外,链路失锁同样也会发生在串行器或者解串器掉电的情况下,掉电的器件重新上电后,链路锁定会自动重新建立,而视频数据和控制通道的配置需要重新写入寄存器。

八哥望春的博客 4960

ThreadExit线程终止(通知方式)

通知方式:线程终止

DingX_001的博客 473

高性能网站设计之缓存更新的套路

看到好些人在写更新缓存数据代码时,先删除缓存,然后再更新数据库,而后续的操作会把数据再装载的缓存中。然而,这个是逻辑是错误的。试想,两个并发操作,一个是更新操作,另一个是查询操作,更新操作删除缓存后,查询操作没有命中缓存,先把老数据读出来后放到缓存中,然后更新操作更新了数据库。于是,在缓存中的数据还是老的数据,导致缓存中的数据是脏的,而且还一直这样脏下去了。 我不知道为什么这么多人用的都是这

代码技巧 1万+

用 SemanticObject 把字段变成可点击链接:Fiori Elements Navigation Using a Link 的工程化实践

SAP Fiori elements 通过 Common.SemanticObject 注解可将字段转为可点击链接,实现应用间跳转。当源字段名与目标属性名不一致时,需使用 Common.SemanticObjectMapping 进行映射。该功能依赖 Fiori Launchpad 的目标映射配置,确保正确解析 semantic object + action 并传递参数。常见误区包括未配置目标映射或字段映射错误,需逐一检查语义标注、intent 解析和参数传递链路。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 278

JAVA线程——Thread 类

Thread 类我们可以理解为是 java 用于管理线程的一个类,里面封装了操作系统提供的线程管理这一方面的 API (Thread 是优化后的结果), Java 代码创建的每一个线程,可以理解为为 Thread 实例化的对象,Thread 对象用于描述线程的信息。

zzbzxzzdf的博客 3194

9.9华为OD机试真题 新系统 - 受限任务分配 (Java/Py/C/C++/Js/Go)

华为OD机试真题 新系统【受限任务分配】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题 【受限任务分配】多语言题解。

qq_45776114的博客 762

Java中Thread详解(一篇就够了)

前言 操作系统中,一个进程往往代表着一个应用程序实例,而线程是进程中轻量级的调度单元,也可以看作是轻量级的进程,可以共享进程资源。下面简单介绍在操作系统中线程通用实现方式。接下来内容主要对线程模型进行简单介绍,然后对Java线程实现Thread类进行了解。 线程模型 暂且抛开Java线程,先说明一下在操作系统中,线程通用的几种实现方式。实现线程主要有三种方式。 内核线程模型 使用内核线程实现的方......

starsray的博客 3万+

基于VLFM的中文指令视觉语言导航系统设计与实现

文章摘要 本文介绍了一个基于VLFM(视觉语言边界地图)的中文指令视觉语言导航系统设计与实现。该系统采用模块化架构,包含三维场景渲染、URDF机器人仿真、VLFM导航引擎、中文指令解析和PyQt图形界面五大核心模块。系统支持自定义GLB场景模型和URDF机器人模型,通过transformers和langchain实现中文自然语言指令解析,利用VLFM算法进行语义导航决策。技术实现上详细介绍了GLB模型加载器开发、URDF机器人控制、VLFM导航算法集成以及PyQt界面设计。该系统在Ubuntu环境下运行,依

AI的内部世界的博客 214

C++(Qt)软件调试---Windows下Qt使用dump定位崩溃位置(1)

在C++/Qt程序崩溃时,往往很难找到崩溃的原因,如果是稳定复现的问题还好,但是多数情况遇见的问题是随机出现的,这时想要快速找到问题并解决就需要通过dump文件定位崩溃位置。

mahuifa的博客 1万+

Java:Thread类及常见方法大全(画图+源码详解)

一、Thread 的常见构造方法 二、Thread 的几个常见属性 ·理解线程是否存活: ·理解前台线程与后台线程: 三、启动一个线程 四、中断一个线程 1、通过共享的标记来进行沟通 2、调用interrupt() 方法来通知 五、等待一个线程 六、获取当前线程的引用 七、休眠当前线程

linsc_05的博客 4669

【GameFramework框架工具链】5-1、DataTable数据表 类自动生成工具

这个是跟【GameFramework框架内置模块】3、数据表(Data Table)模块配合使用的工具链。在使用数据表的时候,我们需要先准备数据表,每个数据表对应一个C#,C#对应表里面的字段,然后使用里面的信息,比如Music数据表ID int类型 IDAssetName string类型 资源名然后,根据数据表生成C#实体类,这一块有一个StarForce的工具链,我们来看一下如何使用。

CSDN博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 1277

JAVA线程之Thread类详解

ss

MC的博客 9万+

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

java的thread类_thread 类详解

java.lang.Thread类详解一、前言位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知识:线程的几种状态、上下文切换,然后接着介绍Thread类中的方法的具体使用。二、线程的状态在正式学习Thread类中的具体方法之前,我们先来了解一下线程有哪些状态,这个将会有助于后面...

weixin_34352565的博客 7865

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Java Thread类详解

Thread类是Java中实现多线程的基类,它继承自Object类。每个线程都有一个对应的Thread对象,通过调用该对象的start()方法来启动线程,调用stop()方法来停止线程。本文详细介绍了Java中Thread类的用法和常见方法,包括创建线程、线程的生命周期、线程同步与通信等。希望对您学习Java多线程编程有所帮助。

我是程序员Amin,关注我,带你学习有趣的计算机知识 604

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Java多线程基础之Thread类详解

一、线程 我们都知道,进程是系统运行程序的基本单位,打开电脑的任务管理器,就可以看到一个个运行中的进程。但是仔细观察可以发现每个进程之下,还有更多的线程。 线程是比进程更小、更轻量级的执行单位,每个进程都拥有自己的一块内存空间和变量资源等,然而同一个进程下的多个线程则共享数据和资源,所以不管线程的创建和销毁工作,还是在线程之间切换工作,都要比进程更加轻量级、消耗系统资源更少。 二、Java多线程编...

学而时‘享’之,乐乎 831

Java中Thread类的详解

Thread(多线程类) 多线程 进程: 通俗的来说,每一个程序都是一个进程,比如:QQ,360,谷歌。 线程: 每一个程序中都有多个功能,每一个功能都可以看成是一个线程。 Thread的使用 继承子类建立线程 对于每一个线程,都是可以通过类继承Thread来完成的。 ublic class Thread_one { public static void main(String[] args) { Ter it = new Ter(); //sta

Beyondandzy的博客 6616

java线程详解01:Thread类详解

本文对Thread类的各个属性,方法进行详细介绍

thinking-fish的博客 1157

Java 多线程基础:Thread 类详解

在 Java 编程中,线程是实现并发的重要工具。通过合理地使用 Thread 类,可以实现多任务并行处理,提升程序的响应能力和执行效率。本文围绕 Thread 类的核心功能展开,详细讲解了线程的创建、终止、等待、休眠等基本操作,同时介绍了如何获取当前线程实例以及线程可能处于的各种状态。文章旨在帮助读者打牢多线程基础,为后续掌握更复杂的并发控制机制(如线程池、同步锁、并发容器等)奠定坚实的理论基础。

Hello World! 1063

Java Thread类主要方法详解

在java中,谈到线程,必然少不了Thread类。线程是比进程更轻量级的调度执行单位。为什么用线程?通过使用线程,可以把操作系统进程的资源分配和执行调度分开,各个线程既可以共享进程资源(内存地址、文件I/O等),又可以独立调度(线程是CPU调度的基本单位)。 主流操作系统(Windows, Linux)都提供了线程的实现,Java则提供了在不同硬件和操作系统下对线程的统一处理,Thread类则是...

茹煜得氺的博客 2万+

Java并发编程之Thread类详解

Thread类详解: 线程(Thread): 是操作系统进行调度的最小单位,Java中的线程是对操作系统线程的封装。本文从线程的创建到停止结合代码和具体实例分析一下关于java线程的一些常见问题。 线程的创建: 自己写一个类继承于java.lang.Thread类,并重写run()接口 实现java.lang.Runnable接口,并传给Thread的构造函数。 //方式1 class MyThread extends Thread{ @Override public void

隐风的博客 3454

Java Thread 类基础用法详解

Java 中通过类来实现线程功能。Thread 类实现了Runnable接口,包含了控制线程生命周期的各种方法。每个线程都是 Thread 类的实例,通过调用start()方法启动。Thread 类是 Java 多线程编程的基础,掌握它对于编写高效并发程序至关重要。线程的基本概念和作用创建线程的两种方式及对比Thread 类的常用方法和示例线程生命周期和状态转换线程安全基础和 synchronized 关键字使用注意事项和最佳实践进阶学习方向线程池(ExecutorService)

2401_89021473的博客 792

Java Thread类的常用方法 详解

java基础(jdk1.8) ** Thread类的常用方法 ** 学习线程先了解Thread类的方法       对于任意一个线程,我们可以使用以下方法来进行调用来完成需求 start() 启动一个新的线程 新线程最终调用runnable接口提供的run方法 run() 主线程直接调用run不会创建新的线程子线程的执行体,子线程run方法结束线程结束 start与run的区别 1.创建线程与否 start创建线程 run不创建线程

qq_43360780的博客 1468

java并发编程Thread类详解

在学习Thread类之前,先介绍与线程相关知识:线程的几种状态、上下文切换,然后接着介绍Thread类中的方法的具体使用。   以下是本文的目录大纲:   一.线程的状态   二.上下文切换   三.Thread类中的方法   若有不正之处,请多多谅解并欢迎批评指正。   请尊重作者劳动成果,转载请标明原文链接:    http://www.cnblogs.com/dolphin05...

189

Java Thread类源码详解

概述 Java所有多线程的实现,均通过封装Thread类实现,所以深入Thread类,对深入理解java多线程很有必要 构造函数: Thread的构造函数,采用缺省的方式实现: //传入Runnable接口实现 Thread(Runnable target) //传入Runnable接口实现,传入线程名 Thread(Runnable target, String name...

有信仰的蜗牛 9805
上一篇: Class类文件详解
下一篇: [java] System类深度解析
topEngineerr
博客等级 码龄11年 627粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址