在讨论多线程之前, 让我们先讨论线程. 线程是进程中轻量级的最小部分, 可以与同一进程的其他部分 (其他线程) 并发运行. 线程是独立的, 因为它们都有独立的执行路径, 这就是为什么如果一个线程中发生异常, 它不会影响其他线程的执行. 进程的所有线程共享公共内存. 同时执行多个线程的过程称为多线程.
让我们把讨论总结成以下几点:
1. 多线程的主要目的是同时执行程序的两个或多个部分, 以最大限度地利用 CPU 时间. 多线程程序包含两个或多个可以并发运行的部分. 程序的每个这样的部分称为线程.
2. 线程是轻量级子进程, 它们共享公共内存空间. 在多线程环境中, 受益于多线程的程序可以利用最大的 CPU 时间, 使空闲时间保持在最小.
3. 线程可以处于以下状态之一:
新 - 尚未启动的线程处于此状态.
RUNNABLE-- 在 Java 虚拟机中执行的线程处于这种状态.
阻塞 -- 等待监视器锁的阻塞线程处于这种状态.
等待 -- 正在无限期等待另一个线程执行特定操作的线程处于这种状态.
TIMED_WAITING - 等待另一个线程执行某个操作长达指定等待时间的线程处于这种状态.
终止 - 已退出的线程处于此状态.
在给定的时间点上, 线程只能处于一种状态.
多任务 vs 多线程 vs 多处理 vs 并行处理
如果您是 java 新手, 您可能会对这些术语感到困惑, 因为在我们讨论多线程时它们经常使用. 让我们简单地谈一谈.
多任务处理: 同时执行多个任务的能力称为多任务处理.
多线程: 我们已经讨论过了. 它是一个同时执行多个线程的进程. 多线程也称为基于线程的多任务处理.
多处理: 它与多任务处理相同, 但是在多处理中涉及多个 CPU. 另一方面, 一个 CPU 参与多任务处理.
并行处理: 它是指在一个计算机系统中使用多个 CPU.
在用 Java 创建线程
在 Java 中有两种创建线程的方法:
1)通过扩展 Thread 类.
2)通过实现 Runnable 接口.
在开始创建线程的程序 (代码) 之前, 让我们先看看 Thread 类的这些方法. 在下面的示例中, 我们很少使用这些方法.
getName(): 用于获取线程的名称
getPriority(): 获取线程的优先级
isAlive(): 确定线程是否仍在运行
join(): 等待线程终止
run(): 线程的入口点
sleep(): 挂起线程一段时间
start(): 通过调用线程的 run()方法来启动线程
方法 1: 通过扩展线程类创建线程 Example 1:
- class MultithreadingDemo extends Thread{
- public void run(){
- System.out.println("My thread is in running state.");
- }
- public static void main(String args[]){
- MultithreadingDemo obj=new MultithreadingDemo();
- obj.start();
- }
- }
- Output:
My thread is in running state.
- Example 2:
- class Count extends Thread{
- Count()
- {
- super("my extending thread");
- System.out.println("my thread created" + this);
- start();
- }
- public void run()
- {
- try
- {
- for (int i=0 ;i<10;i++)
- {
- System.out.println("Printing the count" + i);
- Thread.sleep(1000);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("my thread interrupted");
- }
- System.out.println("My thread run is over" );
- }
- }class ExtendingExample{
- public static void main(String args[])
- {
- Count cnt = new Count();
- try
- {
- while(cnt.isAlive())
- {
- System.out.println("Main thread will be alive till the child thread is live");
- Thread.sleep(1500);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Main thread interrupted");
- }
- System.out.println("Main thread's run is over" );
- }
- }
输出:
my thread createdThread[my runnable thread,5,main]Main thread will be alive till the child thread is livePrinting the count 0Printing the count 1Main thread will be alive till the child thread is livePrinting the count 2Main thread will be alive till the child thread is livePrinting the count 3Printing the count 4Main thread will be alive till the child thread is livePrinting the count 5Main thread will be alive till the child thread is livePrinting the count 6Printing the count 7Main thread will be alive till the child thread is livePrinting the count 8Main thread will be alive till the child thread is livePrinting the count 9mythread run is overMain thread run is over
方法 2: 通过实现 Runnable 接口创建线程
一个简单示例
- class MultithreadingDemo implements Runnable{
- public void run(){
- System.out.println("My thread is in running state.");
- }
- public static void main(String args[]){
- MultithreadingDemo obj=new MultithreadingDemo();
- Thread tobj =new Thread(obj);
- tobj.start();
- }
- }
输出:
My thread is in running state.
示例程序 2:
观察这个程序的输出, 并尝试理解这个程序中发生了什么. 如果您已经理解了每个线程方法的用法, 那么您应该不会遇到任何问题, 请理解这个示例.
- class Count implements Runnable{
- Thread mythread ;
- Count()
- {
- mythread = new Thread(this, "my runnable thread");
- System.out.println("my thread created" + mythread);
- mythread.start();
- }
- public void run()
- {
- try
- {
- for (int i=0 ;i<10;i++)
- {
- System.out.println("Printing the count" + i);
- Thread.sleep(1000);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("my thread interrupted");
- }
- System.out.println("mythread run is over" );
- }
- }class RunnableExample{
- public static void main(String args[])
- {
- Count cnt = new Count();
- try
- {
- while(cnt.mythread.isAlive())
- {
- System.out.println("Main thread will be alive till the child thread is live");
- Thread.sleep(1500);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Main thread interrupted");
- }
- System.out.println("Main thread run is over" );
- }
- }
输出:
my thread createdThread[my runnable thread,5,main]Main thread will be alive till the child thread is livePrinting the count 0Printing the count 1Main thread will be alive till the child thread is livePrinting the count 2Main thread will be alive till the child thread is livePrinting the count 3Printing the count 4Main thread will be alive till the child thread is livePrinting the count 5Main thread will be alive till the child thread is livePrinting the count 6Printing the count 7Main thread will be alive till the child thread is livePrinting the count 8Main thread will be alive till the child thread is livePrinting the count 9mythread run is overMain thread run is over
线程优先级
线程优先级是决定一个线程如何对待其他线程的整数.
线程优先级决定何时从一个正在运行的线程切换到另一个线程, 进程称为上下文切换
线程可以自动释放控制, 准备运行的最高优先级线程是给定 CPU 的.
一个线程可以被一个高优先级线程抢占, 不管低优先级线程在做什么. 当高优先级线程想要运行时, 它就会运行.
要设置线程的优先级, 使用 setPriority()方法, 它是线程类的一个方法.
我们可以使用 MIN_PRIORITY,NORM_PRIORITY 或 MAX_PRIORITY 来代替在整数中定义优先级.
方法: isAlive() 和 join()
在所有实际情况下, 主线程应该是最后一个完成, 其他从主线程派生的线程也会完成.
要知道线程是否已经完成, 我们可以在线程上调用 isAlive(), 如果线程没有完成, 它将返回 true.
另一种方法是使用 join()方法, 当从父线程调用该方法时, 该方法使父线程等待子线程终止.
这些方法是在 Thread 类中定义的.
在上面的例子中, 我们也使用了 isAlive()方法.
同步
多线程为程序引入了异步行为. 如果一个线程正在写一些数据, 那么另一个线程可能正在读取相同的数据. 这可能会带来不一致.
当两个或多个线程需要访问共享资源时, 应该以某种方式让资源一次只被一个资源使用. 实现这一点的过程称为同步.
要实现同步行为, java 有同步方法. 一旦线程位于同步方法中, 其他线程就不能调用同一对象上的任何其他同步方法. 然后所有其他线程等待第一个线程从同步块中出来.
当我们想要同步对一个不是为多线程访问而设计的类的对象的访问时, 并且需要同步访问的方法的代码对我们不可用, 在这种情况下, 我们不能将 synchronized 添加到适当的方法中. 在 java 中, 我们对此有解决方案, 将对这个类定义的方法 (需要同步) 的调用以以下方式放入同步块中.
- Synchronized(object){
- // statement to be synchronized
- }
线程间通信
我们有一些 java 线程可以彼此通信的方法. 这些方法是 wait(),notify(),notifyAll(). 所有这些方法只能从同步方法中调用.
1)了解同步 java 有一个 monitor 的概念. 监视器可以看作是一个只能容纳一个线程的盒子. 一旦一个线程进入监视器, 所有其他线程必须等待该线程退出监视器.
2) wait()告诉调用线程放弃监视器并进入睡眠状态, 直到其他线程进入同一监视器并调用 notify().
3) notify()唤醒同一对象上调用 wait()的第一个线程.
notifyAll()唤醒同一对象上调用 wait()的所有线程. 优先级最高的线程将首先运行.
来源: http://www.jianshu.com/p/f3c714a20fd7