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. 使用Curl进行抓取远程内容时url中文编码问题

    PHP中对于URL进行编码,可以使用 urlencode() 或者 rawurlencode(),二者的区别是前者把空格编码为 '+',而后者把空格编码为 '%20',不过应该注意的是,在编码时应该只 ...

  2. MongoDB -- 更新

    $pull: db.collection.update( <query>, { $pull: { <arrayField>: <query2> } } ) $pul ...

  3. Github Atom

    码代码新神器-Github Atom   周末闲着没事,逛论坛发现了一个新的编辑器,由github发布的Atom编辑器.瞬间被吸引了,所以就去尝试着折腾了一下,后来发现这个编辑器确实很不错,他的特点就 ...

  4. swt,jface,rcp

    //swt-jface-rcp,基本结构:display类,shell类,组件:widget窗口控件,control控件,composites面板,button,label,text文本框,list列 ...

  5. Windbg抓取程序崩溃的dmp文件的方法

    Windbg抓取程序崩溃的dmp文件的方法 一.        简介 windbg是在windows平台下,强大的用户态和内核态调试工具.相比较于Visual Studio,它是一个轻量级的调试工具, ...

  6. C++的for循环细节,必看!

    C++中.For(A;B;C)   C语句是在每次循环后才运行. 如: y=10; for( i=0;i<10;y=++i) {    cout<<y<<endl; } ...

  7. QUIC简单介绍

    QUIC,即Quick UDP Internet Connection,类似于SPDY,相同也是由Google公司在现有已存协议之上进行了扩展设计,而旨在降低网络延迟.之前我曾介绍过SPDY的相关信息 ...

  8. Load and Unload

    一.前言 在前一段时间,我遭遇了一个现象诡异的Bug,最后原因归结为在DllMain里错误地调用了FreeLibrary(在本文最后对此Bug有详细的解释). MSDN里关于禁止在DllMain里调用 ...

  9. Hawk-数据抓取工具

    Hawk-数据抓取工具:简明教程   Hawk: Advanced Crawler& ETL tool written in C#/WPF 1.软件介绍 HAWK是一种数据采集和清洗工具,依据 ...

  10. web框架python

    22.python笔记之web框架   一.web框架本质 1.基于socket,自己处理请求 #!/usr/bin/env python3 #coding:utf8 import socket de ...