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线程例子登山

时间: 2024-10-09 08:32:47

java线程例子登山的相关文章

Java线程学习经典例子-读写者演示

Java线程学习最经典的例子-读写者,主要用到Thread相关知识如下: -         线程的start与run -         线程的休眠(sleep) -         数据对象加锁(synchronized) -         数据对象的等待与释放(wait and notify) 程序实现: -ObjectData数据类对象,通过synchronized关键字实现加锁,在线程读写者中使用. -ConsumerThread消费者线程,读取数据对象中count值之后,通知生产者

java线程及操作实例,线程池简单例子

java io.集合.线程.字符串.gc.jvm可谓是java中的最基本的知识,尤其是线程操作复杂,相应难懂,要想java基础知识扎实,上面提到的几个方面的知识点都要精通,这样方可以称自己掌握java方面基础知识. 总结一下java线程知识,平时接触过线程,尤其是在android开发中,线程可谓是无处不在,稍有不注意就会报错.在java中线程也是无处不在,main就是一个线程,只不过被包装好了,一般接触不到. 我的无数次的复习经历告诉我,学习知识最快,最深刻的方法就是从解题开始,不要先看概念,遇

Java线程使用大全

1.线程实现 1.Thread类 构造方法: 案例代码: public class Ex10_1_CaseThread extends Thread {// 创建一个类继承(extend)Thread类 String studentName; public Ex10_1_CaseThread(String studentName) {// 定义类的构造函数,传递参数 System.out.println(studentName + "申请访问服务器"); this.studentNam

java线程详细介绍

目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么要用join方法 七常见线程名词解释 八线程同步 九线程数据传递 本文主要讲了java中多线程的使用方法.线程同步.线程数据传递.线程状态及相应的一些线程函数用法.概述等. 首先讲一下进程和线程的区别: 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1

Java线程经典面试题

53道Java线程面试题 下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务提速.比如,如果一个线程完成一个任务要100毫秒,那么用十个线程完成改任务只需10毫秒.Java在语言层面对多线程提供了卓越的支持,它也是一个很好的卖点. 2) 线程和进程有什么区别? 线程是进程的子集,一个进程可以有很多线程,每条线程并

Java线程面试题 Top 50

原文链接: javarevisited 翻译: ImportNew.com- 李 广译文链接: http://www.importnew.com/12773.html 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程技术并且有丰富的Java程序开发.调试.优化经验,所以线程相关的问题在面试中经常会被提到. 在典型的Java面试中, 面试官会从线程

Java深度历险(三)——Java线程?:基本概念、可见性与同步

开发高性能并发应用不是一件容易的事情.这类应用的例子包括高性能Web服务器.游戏服务器和搜索引擎爬虫等.这样的应用可能需要同时处理成千上万个请求.对于这样的应用,一般采用多线程或事件驱动的架构.对于Java来说,在语言内部提供了线程的支持.但是Java的多线程应用开发会遇到很多问题.首先是很难编写正确,其次是很难测试是否正确,最后是出现问题时很难调试.一个多线程应用可能运行了好几天都没问题,然后突然就出现了问题,之后却又无法再次重现出来.如果在正确性之外,还需要考虑应用的吞吐量和性能优化的话,就

Java线程:锁

一.锁的原理 Java中每个对象都有一个内置锁,当程序运行到非静态的synchronized同步方法上时,自动获得与正在执行的代码类的当前实例(this实例)有关的锁.获得一个对象的锁也称为获取锁.锁定对象.在对象上锁定或在对象上同步. 当程序运行到synchronized同步方法或代码块时该对象锁才起作用.一个对象只有一个锁.所以一个线程获得该所,就没有其他线程获得,直到第一个线程释放(或返回)锁.这也意味着任何其他线程都不能进入该对象上的synchronized方法或代码块,直到该锁被释放.

【转】Java线程面试题Top50

目录(?)[-] 50道Java线程面试题 1 什么是线程 2 线程和进程有什么区别 3 如何在Java中实现线程 4 用Runnable还是Thread 6 Thread 类中的start 和 run 方法有什么区别 7 Java中Runnable和Callable有什么不同 8 Java中CyclicBarrier 和 CountDownLatch有什么不同 9 Java内存模型是什么 10 Java中的volatile 变量是什么 11 什么是线程安全Vector是一个线程安全类吗 详见这