java中创建执行线程的两种方法
继承Thread类。
public class MyThread extends Thread {
@Override
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + " 运行了");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
while (true) {
System.out.println("主线程 "+Thread.currentThread().getName() + " 运行了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

实现Runnable接口。
public class MyThread extends Thread {
@Override
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + " 运行了");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
while (true) {
System.out.println("主线程 "+Thread.currentThread().getName() + " 运行了");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
 当调用 start()方法时将创建新的线程,并且执行在 run()方法里的代码。 但是如果直接调用 run()方法,它不会创建新的线程也不会执行调用线程的代码。
java线程的特征和状态
所有的Java程序,不论并发与否,都有一个名为主线程的Thread对象。执行该程序时,Java虚拟机(JVM)将创建一个新Thread并在该线程中执行main()方法。这是非并发应用程序中唯一的线程,也是并发应用程序中的第一个线程。
Java中的所有线程都有一个优先级,这个整数值介于Thread.MIN_PRIORITY(1)和Thread.MAX_PRIORITY(10)之间,默认优先级是Thread.NORM_PRIORITY(5)。线程的执行顺序并没有保证,通常,较高优先级的线程将在较低优先级的钱程之前执行。
在Java中,可以创建两种线程:,守护线程和非守护线程。守护线程通常用在作为垃圾收集器或缓存管理器的应用程序中,执行辅助任务。在线程start之前调用isDaemon()方法检查线程是否为守护线程,也可以使用setDaemon()方法将某个线程确立为守护线程。
Java程序结束执行过程的情形:
- 程序执行Runtime类的exit()方法, 而且用户有权执行该方法。
- 应用程序的所有非守护线程均已结束执行,无论是否有正在运行的守护线程
线程的状态(Thread.States类):
NEW:Thread对象已经创建,但是还没有开始执行。 RUNNABLE:Thread对象正在Java虚拟机中运行。 BLOCKED : Thread对象正在等待锁定。 WAITING:Thread 对象正在等待另一个线程的动作。 TIME_WAITING:Thread对象正在等待另一个线程的操作,但是有时间限制。 TERMINATED:Thread对象已经完成了执行。
Thread中的常用方法
- getId():该方法返回Thread对象的标识符。该标识符是在钱程创建时分配的一个正整数。在线程的整个生命周期中是唯一且无法改变的。
- getName()/setName():这两种方法允许你获取或设置Thread对象的名称。这个名称是一个String对象,也可以在Thread类的构造函数中建立。
- getPriority()/setPriority():你可以使用这两种方法来获取或设置Thread对象的优先级。
- isDaemon()/setDaemon():这两种方法允许你获取或建立Thread对象的守护条件。
- getState():该方法返回Thread对象的状态。
- interrupt():中断目标线程,给目标线程发送一个中断信号,线程被打上中断标记。
- interrupted():判断目标线程是否被中断,但是将清除线程的中断标记。
- isinterrupted():判断目标线程是否被中断,不会清除中断标记。
- sleep(long ms):该方法将线程的执行暂停ms时间。
- join():暂停线程的执行,直到调用该方法的线程执行结束为止。可以使用该方法等待另一个Thread对象结束。
- setUncaughtExceptionHandler():当线程执行出现未校验异常时,该方法用于建立未校验异常的控制器。
- currentThread():Thread类的静态方法,返回实际执行该代码的Thread对象。
Callable
Callable接口是一个与Runnable接口非常相似的接口,但是它可以定义返回值,主要特征有
- 接口。有简单类型参数,与call()方法的返回类型相对应
- 声明了call()方法。执行器运行任务时,该方法会被执行器执行。它必须返回声明中指定类型的对象。
- call()方法可以抛出任何一种校验异常。可以实现自己的执行器并重载afterExecute()方法来处理这些异常。
代码实现
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("call方法执行");
Thread.sleep(2000);
return "hello world call() invoked!";
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10)) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
System.out.println("异常信息:"+t);
}
};
Future<String> future = executor.submit(new MyCallable());
String s = future.get();
System.out.println(s);
executor.shutdown();
}
}

|