java中的多线程的示例

作者 : 开心源码 本文共5222个字,预计阅读时间需要14分钟 发布时间: 2022-05-12 共159人阅读

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

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

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

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » java中的多线程的示例

发表回复