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线程例子登山的更多相关文章

  1. 线程和线程池的理解与java简单例子

    1.线程 (1)理解,线程是系统分配处理器时间资源的基本单元也是系统调用的基本单位,简单理解就是一个或多个线程组成了一个进程,进程就像爸爸,线程就像儿子,有时候爸爸一个人干不了活就生了几个儿子干活,会 ...

  2. 【转载】 Java线程面试题 Top 50

    Java线程面试题 Top 50 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员 的欢迎.大多数待遇丰厚的J ...

  3. 第22章 java线程(2)-线程同步

    java线程(2)-线程同步 本节主要是在前面吃苹果的基础上发现问题,然后提出三种解决方式 1.线程不安全问题 什么叫线程不安全呢 即当多线程并发访问同一个资源对象的时候,可能出现不安全的问题 对于前 ...

  4. 第21章 java线程(1)-线程初步

    java线程(1)-线程初步 1.并行和并发 并行和并发是即相似又有区别: 并行:指两个或者多个事件在同一时刻点发生. 并发:指两个或多个事件在同一时间段内发生 在操作系统中,并发性是指在一段事件内宏 ...

  5. Java线程:线程的同步-同步方法

    Java线程:线程的同步-同步方法   线程的同步是保证多线程安全访问竞争资源的一种手段. 线程的同步是Java多线程编程的难点,往往开发者搞不清楚什么是竞争资源.什么时候需要考虑同步,怎么同步等等问 ...

  6. Java线程新特征——Java并发库

    一.线程池   Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定 ...

  7. Java线程池使用说明

    Java线程池使用说明 转自:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极 ...

  8. Java线程:概念与原理

    Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程 ...

  9. Java线程的5个使用技巧

    萝卜白菜各有所爱.像小编我就喜欢Java.学无止境,这也是我喜欢它的一个原因.日常工作中你所用到的工具,通常都有些你从来没有了解过的东西,比方说某个方法或者是一些有趣的用法.比如说线程.没错,就是线程 ...

随机推荐

  1. hdu2066一个人的旅行

    枚举全部相邻城市,作为起点,多次spfa,然后每次在想去的城市中找出spfa后的距离起点最短的花费时间 #include <iostream> #include <cstring&g ...

  2. IE6下position:fixed不支持问题及其解决方式

    IE6有诸多奇葩,不支持position:fixed就是当中之中的一个.所以在做一些比方固定在顶部或者底部或者固定元素的效果时须要考虑兼容IE6的这个问题.解决方式是用Ie6的hack. *html ...

  3. Missile:双状态DP

    题目 描写叙述 Long , long ago ,country A invented a missile system to destroy the missiles from their enem ...

  4. 从Hadoop骨架MapReduce在海量数据处理模式(包括淘宝技术架构)

    从hadoop框架与MapReduce模式中谈海量数据处理 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显兴奋,认为它们非常是神奇.而神奇的东西常能勾 ...

  5. java学习笔记05--字符串 .

    java学习笔记05--字符串 . 一.String类 由字符所组成的一串文字符号被称之为字符串.在java中字符串不仅仅是字符数组,而且是String类的一个实例,可以使用String类来构建. 字 ...

  6. AIX下RAC搭建 Oracle10G(四)安装CRS

    AIX下RAC搭建系列 AIX下RAC搭建Oracle10G(四)安装CRS 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX2 ...

  7. Installing SSL on CentOS | My Virtual Time Capsule

    Installing SSL on CentOS | My Virtual Time Capsule Installing SSL on CentOS Extracted from the Sourc ...

  8. PHP操作Mysql中间BLOB场

    1.MySQL在BLOB字段类型 BLOB场的类型用于存储二进制数据. MySQL在.BLOB它是一种类型的一系列.含有:TinyBlob.Blob.MediumBlob.LongBlob.大小上不同 ...

  9. Graphical Shell with WebShell - WebOS Internals

    Graphical Shell with WebShell - WebOS Internals Graphical Shell with WebShell From WebOS Internals J ...

  10. 【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的加入删除、数据存储等

    (1)效果 (2)源码与第三方类库下载 http://download.csdn.net/detail/wsb200514/8155979 (3)总结 --导航控制器,能够直接用代码的push和pop ...