本文共 5670 字,大约阅读时间需要 18 分钟。
操作系统支持同时运行多个任务,每个任务通常是一个程序,所有运行中的程序称为进程。进程内部包含多个顺序执行流,每个流称为线程。多线程允许多个任务同时运行,特别是在多核CPU系统中,每个CPU都可以同时处理多个任务。
线程调度有两种方式:
设置线程优先级可通过任务管理器打开详细信息,右键设置线程优先级。
Java中,java.lang.Thread类代表线程。所有线程类必须继承Thread或实现Runnable接口。
Runnable接口run()方法。start()方法启动线程。示例:
public class Demo04 implements Runnable { @Override public void run() { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + i); } }}public class test1 { public static void main(String[] args) { Demo04 obj = new Demo04(); Thread thread1 = new Thread(obj, "线程"); thread1.start(); }} public class Demo05 { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }; new Thread(r).start(); for (int i = 0; i < 4; i++) { System.out.println(Thread.currentThread().getName() + i); } }} 线程安全指多线程同时运行,结果与单线程相同。解决方法是通过同步机制。
synchronized关键字。synchronized修饰方法。Lock对象。示例:
public class Demo07 implements Runnable { private int num = 10; Object lock = new Object(); @Override public void run() { while (true) { synchronized (lock) { if (num > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } System.out.println(Thread.currentThread().getName() + "数字为:" + num--); } } } }}class test2 { public static void main(String[] args) { Demo07 x = new Demo07(); Thread a = new Thread(x, "线程a"); Thread b = new Thread(x, "线程b"); a.start(); b.start(); }} public class Demo09 implements Runnable { private int num = 10; Lock lock = new ReentrantLock(); @Override public void run() { while (true) { lock.lock(); try { if (num > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } lock.unlock(); System.out.println(Thread.currentThread().getName() + "数字为:" + num--); } } finally { lock.unlock(); } } }}class test5 { public static void main(String[] args) { Demo09 obj = new Demo09(); Thread thread = new Thread(obj, "线程x"); thread.start(); }} 线程状态包括:NEW、Runnable、Blocked、Waiting、Timed Waiting、Terminated。
public class Demo10 extends Thread { public void run() { for (int i = 0; i < 100; i++) { if (i % 10 == 0) { System.out.println("——————" + i); } System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new Demo10().start(); }} public class Demo11 { public static Object obj = new Object(); public static void main(String[] args) { Thread waitingThread = new Thread(new Runnable() { @Override public void run() { while (true) { synchronized (obj) { System.out.println(Thread.currentThread().getName() + "获取到锁对象,调用wait方法"); obj.wait(); System.out.println(Thread.currentThread().getName() + "从waiting状态醒来"); } } } }, "等待线程").start(); Thread awakeningThread = new Thread(new Runnable() { @Override public void run() { while (true) { System.out.println(Thread.currentThread().getName() + "——等待3秒钟"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (obj) { System.out.println(Thread.currentThread().getName() + "获取到锁对象,调用notify方法"); obj.notify(); } } } }, "唤醒线程").start(); }} 线程池用于高效管理多线程任务,避免频繁启动线程。
public class Demo12_ThreadPool { public static void main(String[] args) throws Exception { ExecutorService pool = Executors.newFixedThreadPool(6); Runnable obj1 = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "执行"); } }; Runnable obj2 = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "执行"); } }; pool.submit(obj1); pool.submit(obj2); pool.shutdown(); }} 线程池提供了灵活的任务处理方式,适合高并发环境。通过合理配置线程池大小,可以优化系统性能。
转载地址:http://qzxkz.baihongyu.com/