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

标签:Java,Thread,Runnable,线程
0
投稿

猜你喜欢

  • Spring Cache抽象-使用SpEL表达式解析

    2023-08-23 11:46:44
  • java定义数组的三种类型总结

    2022-06-27 01:44:44
  • C#中using指令的几种用法

    2022-01-25 01:35:25
  • 设置Android设备WIFI在休眠时永不断开的代码实现

    2022-08-26 09:03:00
  • C# goto语句的具体使用

    2021-07-22 22:26:22
  • 利用Thumbnailator轻松实现图片缩放、旋转与加水印

    2022-03-26 18:44:04
  • 总结的5个C#字符串操作方法分享

    2022-08-10 04:02:01
  • Java序列化常见的三个问题

    2023-11-21 08:40:52
  • Spring启动过程中实例化部分代码的分析之Bean的推断构造方法

    2022-08-26 02:00:07
  • 在Struts2中如何将父类属性序列化为JSON格式的解决方法

    2022-08-01 09:30:34
  • springboot结合websocket聊天室实现私聊+群聊

    2022-09-25 03:22:23
  • C# 获取某个时间的0点0分和23点59分59秒

    2023-03-02 10:05:18
  • Android 中 ActivityLifecycleCallbacks的实例详解

    2022-08-12 06:03:34
  • Android 弹出软键盘所遇到的坑及解决方法

    2022-06-03 23:41:47
  • java通过方向键控制小球移动的小游戏

    2023-11-10 05:25:59
  • Java程序中实现调用Python脚本的方法详解

    2021-07-08 22:00:08
  • C#使用WebSocket实现聊天室功能

    2022-08-10 09:29:33
  • Java与C++实现相同的MD5加密算法简单实例

    2023-08-31 09:43:02
  • Java中的对称加密详解

    2023-09-30 12:17:16
  • SpringMvc后台接收json数据中文乱码问题详解

    2022-12-03 00:08:58
  • asp之家 软件编程 m.aspxhome.com