Java使用Thread和Runnable的线程实现方法比较
作者:cakincqm 发布时间:2021-11-17 07:52:54
本文实例讲述了Java使用Thread和Runnable的线程实现方法。分享给大家供大家参考,具体如下:
一 使用Thread实现多线程模拟铁路售票系统
1 代码
public class ThreadDemo
{
public static void main( String[] args )
{
TestThread newTh = new TestThread( );
// 一个线程对象只能启动一次
newTh.start( );
newTh.start( );
newTh.start( );
newTh.start( );
}
}
class TestThread extends Thread
{
private int tickets = 5;
public void run( )
{
while( tickets > 0 )
{
System.out.println( Thread.currentThread().getName( ) + " 出售票 " + tickets );
tickets -= 1;
}
}
}
2 运行
Thread-0 出售票 5
Thread-0 出售票 4
Thread-0 出售票 3
Thread-0 出售票 2
Thread-0 出售票 1
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at ThreadDemo.main(ThreadDemo.java:16)
3 说明
一个线程只能启动一次
二 main方法中产生4个线程
1 代码
public class ThreadDemo
{
public static void main(String[]args)
{
// 启动了四个线程,分别执行各自的操作
new TestThread( ).start( );
new TestThread( ).start( );
new TestThread( ).start( );
new TestThread( ).start( );
}
}
class TestThread extends Thread
{
private int tickets = 5;
public void run( )
{
while (tickets > 0)
{
System.out.println(Thread.currentThread().getName() + " 出售票 " + tickets);
tickets -= 1;
}
}
}
2 运行
Thread-0 出售票 5
Thread-0 出售票 4
Thread-0 出售票 3
Thread-0 出售票 2
Thread-0 出售票 1
Thread-1 出售票 5
Thread-1 出售票 4
Thread-1 出售票 3
Thread-1 出售票 2
Thread-1 出售票 1
Thread-2 出售票 5
Thread-2 出售票 4
Thread-2 出售票 3
Thread-2 出售票 2
Thread-2 出售票 1
Thread-3 出售票 5
Thread-3 出售票 4
Thread-3 出售票 3
Thread-3 出售票 2
Thread-3 出售票 1
三 使用Runnable接口实现多线程,并实现资源共享
1 代码
public class RunnableDemo
{
public static void main( String[] args )
{
TestThread newTh = new TestThread( );
// 启动了四个线程,并实现了资源共享的目的
new Thread( newTh ).start( );
new Thread( newTh ).start( );
new Thread( newTh ).start( );
new Thread( newTh ).start( );
}
}
class TestThread implements Runnable
{
private int tickets = 5;
public void run( )
{
while( tickets > 0 )
{
System.out.println( Thread.currentThread().getName() + " 出售票 " + tickets );
tickets -= 1;
}
}
}
2 运行
Thread-0 出售票 5
Thread-0 出售票 4
Thread-0 出售票 3
Thread-0 出售票 2
Thread-0 出售票 1
更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/95664631
猜你喜欢
- 目录前言准备工作Nacos安装及使用入门准备三个SpringBoot服务,引入Nacos及Kafka业务解读Nacos配置创建配置读取配置监
- 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力。下面的章节分为上下两篇,
- 在Servlet 3.0之前都是使用web.xml文件进行配置,需要增加Servlet、Filter或者Listener都需要在web.xm
- 本文实例为大家分享了Android播放音乐案例的具体实现代码,供大家参考,具体内容如下效果:分析:和上一篇文章的结构是一样的,只不过我们需要
- 最近项目中使用springboot+jwt实现登录权限控制,所以在这里记录一下防止以后忘记,毕竟好记性不如烂笔头嘛~。首先我们需要导入使用到
- 实体类时间格式化java 实体类 时间格式化注解@JsonFormat(pattern = "yyyy-MM-dd HH:mm:s
- springboot项目启动的时候参数无效今天启动一个springboot项目发现启动的时候输入的参数都是不能生效,但是yaml文件的配置却
- 在逆向一个Android程序时,如果只是盲目的分析需要阅读N多代码才能找到程序的关键点或Hook点,本文将分享一下如何快速的找到APP程序的
- fork()函数用于从已存在的进程中创建一个新进程。新进程称为子进程,而园进程称为父进程。使用fork()函数得到的子进程是父进程的一个复制
- springboot项目没有mainClass实现打包运行项目分为两个部分,一个是业务代码模块,一个是框架模块,运行class放在框架部分,
- 前言:我们进行用maven 开发组件的时候,经常会遇到一种情况,我们添加一些maven依赖后,经常会出现本地原本正常的代码编译不过去下面我们
- 一、Spring Boot 、 Spring MVC 、Spring对比首先你需要明白一件事情:Spring Boot项目目的并不是替换Sp
- 效果展示在实际项目当中我们经常看到如下各种剪裁形状的效果,Flutter 为我们提供了非常方便的 Widget 很轻松就可以实现,下面我们来
- 简介最近几年,各种新的高效序列化方式层出不穷,不断刷新序列化性能的上限,最典型的包括:专门针对Java语言的:Kryo,FST等等跨语言的:
- 一、JavaMail API简介 JavaMail API是读取、撰写、发送电子信息的可选包。我们可用它来建立如Eudora、Foxmail
- 文件目录结构文件目录结构很重要,特别注意的是rule文件要放在主启动类上一级位置,才能够扫描。写pom<dependencies>
- 前提之前很长一段时间关注JDK协程库的开发进度,但是前一段时间比较忙很少去查看OpenJDK官网的内容。Java协程项目Loom(因为项目还
- 或是将Datagrid或是Gridview的输出导出,实现大体上又分为调用COM+组件或是利用Response(当然是B/S架构的项目)的输
- 四大函数式接口新时代的程序员:lambda 表达式,链式编程,函数式接口,Stream 流式计算函数式接口: 只有一个方法的接口@Funct
- 委托定义如下:public class SocketSp{ public delegate void ReceiveComplet