`
guoyiqi
  • 浏览: 1010763 次
社区版块
存档分类
最新评论

java线程例子登山

 
阅读更多

Through its implementation, this project will familiarize you with the creation and execution of threads, and with the use of the Thread class methods. In order to synchronize the threads you will have to use (when necessary), run( ), start( ), currentThread( ), getName( ), join( ), yield( ), sleep(time), isAlive( ), getPriority( ), setPriority( ), interrupt( ).

In synchronizing threads, do NOT use any semaphores, wait( ), notify( ) or notifyAll( ).

Triathlon

The triathlon competitors arrive at the stadium. There are three events that must be completed: running, bicycle riding and swimming. Once arrived, the competitors must wait (use busy waiting) for the race to start. The judge will announce the start of the race (use Boolean shared variable start) and mark the starting time in a database (for obtaining the time use the age( ) method). Each competitor runs for a random time between 20 and 40 units.

NOTE: starting with JDK 1.5 and up, operations on vectors are already synchronized, and executed in a Mutual Exclusion fashion. After a competitor completes the run, the time must be registered in the database (don’t need to have a real database but you need to save the necessary information). Between the events, each competitor is allowed to take a break of a random time of up to 10 units. Generate a rest period between 1 and 13 units (use sleep(randomTime)). If a competitor exceeds the allowed time, he will be disqualified. The next course is the bicycle event. All of the bicycles are stored at the starting location. Each competitor will wait (using busy waiting) for a bicycle to be available for pick up. The judge will instruct the competitors in a FCFS order to pick up their bikes. Preparing the bicycle takes some time. The competitor wants to prepare the bicycle as fast as he can. For five units of time only the competitor will increase its priority by 2 (use getPriority( ), setPriority( )). When ready to start the race, each competitor will log the starting time and bicycle for a random time between 30 and 45 units. When the competitor gets to the finish line, he will log the finish time in the database.

A new rest break will be allowed (implement it using the yield( ) method). The last course is the swimming event. It starts once the competitors finish the rest break following the run. There are numLanes available. Competitors will have to form up into groups of size equal to numLanes. When a group is ready, the race can start. The starting time must be registered in the database. Each involved competitor will swim for a random time between 10 and 15 units. Once the competitor finishes the race, he will register the time in the database.

Note: don’t worry about the possibility at some point of having two sets of swimmers in the swimming pool at the same time. Consider that each group uses a different swimming pool.

The competitors must wait until the judge announces the prizes (simulate wait using sleep of a long time - like 500 units, longer that the time it takes the judge to do the all computations). The last competitor to finish the race must inform the judge that the race is over before going to sleep. Use a counter to keep track of the number of competitors that have finished. The judge will resume all the competitors (by interrupting them from sleep – use interrupt( )) when done with the computations, and will announce the first prize for each event and the grand prize for the race. The competitors will leave one by one, in the order of their ID(use isAlive( ) and join( )). In your implementation you should also make use of the currentThread( ) and getName( ) methods.

Using Java programming, synchronize the two types of threads, judge, competitors, in the context of the problem. Closely follow the implementation requirements.

The number of competitors, numComp, numLanes should be read as command line arguments, e.g. –c <int> -l <int>.

Default values: numComp = 20

numLanes = 4

Use appropriate System.out.println( ) statements and the age( ) method to reflect the time of each particular action done by a specific thread. This is necessary for us to observe how the synchronization is working.

Note: a good value for 1 unit of time might be: 100ms

• Add the following lines to all the threads you make:

public static long time = System.currentTimeMillis();

public void msg(String m) {

System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);

}

Whenever you want to print something from that thread do: msg("some message here");

• NAME YOUR THREADS or the above lines that were added would mean nothing. Here's how the constructors should look like (you may use any variant of this as long as each thread is unique and distinguishable):

// Default constructor

public RandomThread(int id) {

setName("RandomThread-" + id);

 

分享到:
评论

相关推荐

    java多线程例子

    Java 多线程例子 Java 多线程是 Java 编程语言中的一种基本概念,它允许程序同时执行多个任务,从而提高程序的性能和效率。本文将通过三个实用的例子,讲解 Java 多线程的基本概念和使用方法。 第一个例子:创建多...

    java线程例子大全

    本资源“java线程例子大全”包含了十八个单元的实例代码,覆盖了Java线程操作的多个方面。 1. **线程创建** - 继承`Thread`类:创建一个新的类,该类继承自`Thread`,然后重写`run()`方法。实例化后调用`start()`...

    java线程例子程序

    本示例程序"java线程例子程序"展示了如何启动并管理多个线程,同时计算它们的运行时间和总挂起时间。 首先,我们来看`ThreadPrj.java`,这个文件很可能包含了线程类的定义。在这个类中,通常会重写`run()`方法,该...

    java线程实例 各种小Demo

    Java线程是多任务编程的重要概念,它允许程序同时执行多个独立的任务,从而提高系统效率和响应速度。在Java中,线程可以分为用户线程和守护线程,前者是程序运行的基础,而后者是在所有用户线程结束时才终止的后台...

    java线程分析java project例子

    java线程分析java project例子,里面分析了sleep(),join(),yield()和wait以及notify等方法的使用以及需要注意的地方。

    java 线程工具类 java 线程工具类

    java 线程工具类 java 线程工具类java 线程工具类 java 线程工具类java 线程工具类 java 线程工具类java 线程工具类 java 线程工具类java 线程工具类 java 线程工具类java 线程工具类 java 线程工具类java 线程工具...

    java多线程经典例子

    在Java多线程编程中,理解如何创建和...总的来说,这个例子展示了Java多线程的基本操作,包括创建、启动、管理和通信。理解和掌握这些概念对于进行并发编程是至关重要的,可以帮助开发者构建高效、稳定的多线程应用。

    经典线程例子——Java线程学习指南

    Java线程是并发编程的核心部分,它允许程序同时执行多个任务,从而提高系统效率...实践中的线程例子将有助于巩固理论知识,加深对线程工作原理的把握。在学习过程中,不断编写和调试多线程程序,是提升技能的最佳途径。

    java多线程例子(1).pdf

    java多线程例子(1).pdf java多线程例子(1).pdf java多线程例子(1).pdf

    Java线程的简单例子

    Java线程是Java编程中的重要概念,它允许程序同时执行多个任务,极大地提高了程序的并发性和效率。在Java中,线程是程序执行的最小单位,由Java虚拟机(JVM)来管理和调度。理解并熟练掌握Java线程的创建、控制以及...

    java中线程的应用例子

    Java中的线程模型是建立在操作系统级别的线程之上的,因此,通过Java的线程机制,开发者可以充分利用多核处理器的资源,提高程序的并发性和响应速度。下面我们将深入探讨Java中线程的应用实例,以及在实际开发中需要...

    Java线程状态流转图

    Java线程状态流转图知识点总结 Java线程状态流转图是一种用于描述Java线程生命周期中不同的状态和状态转换的图形表示方式。该图形展示了Java线程从创建到终止的整个生命周期,并详细介绍了每种状态的特点和转换...

    java多线程的一个简单例子

    很简单的一个java多线程例子,希望多大家有帮助

    计算机二级Java线程例子学习教程.pdf

    Java线程是并发编程的基础,尤其对于计算机二级考试来说,理解和掌握Java线程的使用至关重要。线程使得程序能够同时处理多个任务,提高了系统资源的利用率和程序的响应速度。 在Java中,线程有两种创建方式:显式...

    java一个多线程的经典例子

    根据提供的文件信息,我们可以归纳出以下关于Java多线程的经典示例中的关键知识点: ### Java多线程实现方式 在Java中,实现多线程有两种主要的方法:通过继承`Thread`类或者实现`Runnable`接口。 #### 继承...

    Java线程详解大全

    Java线程是并发编程的核心部分,它允许程序在同一时间执行多个独立的任务,从而提高系统效率和响应速度。本文将深入探讨Java线程的概念、生命周期、实现方式以及相关的同步机制。 首先,理解线程的基本概念至关重要...

    java 线程 dump 分析工具 2.3.3

    java 线程Dump 分析工具: Java的TDA线程转储分析器是一个用于分析Sun Java VM生成的线程转储和堆信息的小型Swing GUI(目前用1.4测试)。它从提供的日志文件中解析线程转储和类直方图。它提供关于发现的线程转储的...

    java 线程相关工具类

    java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类.java 线程相关工具类....

    2012年计算机二级Java线程例子学习教程.pdf

    Java线程是并发编程的基础,尤其对于计算机二级考试来说,理解和掌握Java线程的使用至关重要。在Java中,线程可以使得程序具有更好的响应性和效率,因为它允许程序同时执行多个任务。 7.2.1 显式定义线程 在Java...

    Java线程(第三版)

    《Java线程(第三版)》是一本深入探讨Java线程技术的专业书籍,旨在帮助开发者理解和掌握Java平台上的多线程编程。Java线程是并发编程的重要组成部分,它允许程序同时执行多个任务,从而充分利用系统资源,提高程序的...

Global site tag (gtag.js) - Google Analytics