美文网首页程序员Android
java中的多线程的示例

java中的多线程的示例

作者: 墨雨轩夏 | 来源:发表于2019-01-26 18:59 被阅读1次

    在讨论多线程之前,让我们先讨论线程。线程是进程中轻量级的最小部分,可以与同一进程的其他部分(其他线程)并发运行。线程是独立的,因为它们都有独立的执行路径,这就是为什么如果一个线程中发生异常,它不会影响其他线程的执行。进程的所有线程共享公共内存。同时执行多个线程的过程称为多线程。

    让我们把讨论总结成以下几点:

    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()的所有线程。优先级最高的线程将首先运行。

     为了让学习变得轻松、高效,今天给大家免费分享一套Java教学资源。帮助大家在成为Java架构师的道路上披荆斩棘。需要资料的欢迎加入学习交流群:9285,05736

    相关文章

      网友评论

        本文标题:java中的多线程的示例

        本文链接:https://www.haomeiwen.com/subject/jyzijqtx.html