Results 1 to 8 of 8
  1. #1
    mengqi68 is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Java Input/Output program using threads

    Hi I am trying to write a program in Java that uses threads. I have no idea where to start. Thanks for helping.

    Below is the program requirements:

    The goal of this assignment is to create a routine which creates multiple threads, has them do work in parallel, and terminates when the last thread has finished.

    The Scenario: There are several groups of people in a bar watching the Olympics cheering for their country. Each group will cheer for their country some given number of times, with a random pause (between 2 and 5 seconds) between each cheer. There is enough room at the bar for up to ten different groups to sit (each would be cheering for a different country).

    The Program: The task is to write a program that will simulate these cheers using threads. The program should be called cheer.X (X being the language of choice). You may use any language that supports threading. When the program is run it should ask for the number of countries and then the name and how many times it will be cheered for. The main function will then create a thread for each team and each thread is responsible for cheering the specified number of times for the correct team at the random interval. You will submit the proper source code file for me to open and compile myself, not an executable.

    An example cheer would be: “Go USA!” An example run would look something like this: How many countries are supported at the bar? 3 Enter the first: China How many cheers? 2 Enter the second: USA How many cheers? 4 Enter the third: Russia How many cheers? 3

    Go USA! Go China! Go Russia! Go China! Go USA! Go Russia! Go USA! Go Russia! Go USA! (Remember there will be a pause, and consecutive runs with the same arguments will not create the same results)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Input/Output program using threads

    The assignment text doesn't mention any synchronization between threads, so two (or more) threads are allowed to cheer at the same time. One round of cheering goes like this:

    1) wait for 2 ... 5 seconds;
    2) cheer.

    Sticking the above in a loop completes the entire task for a single thread. Define a couple of those threads and go for it (that's all, really).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java Input/Output program using threads

    Hi,

    If you do not know where to start then I suggest to simply search on google for an introduction. So an introduction to java threading could be:
    Introduction to Java threads | JavaWorld
    But please be aware that there are a lot more tutorials available and I just gave the first search hit.

    If you also need help on other java basics, you could even search on other topics or just read a generic java introduction.

    If you encounter some trouble (e.g. error messages when compiling or your code is not working as you expect it) and you cannot solve it on your own: feel free to give us details and we might be able to help you further.

    With kind regards,

    Konrad

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Java Input/Output program using threads

    Basically what you're saying is "you can google it". And you're correct, but in the case of threading that won't help you THAT much since threading requires a great deal of vision and understanding to do right; the code is -really- simple, the problem domain on the other hand is -very- difficult. Mere tutorials don't really provide what you need. That article you linked to for example is very technical in nature and accurately describes how to use Java's simple API interface to threading; but does not paint any picture that helps you to understand the problems you have to solve to do it right.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java Input/Output program using threads

    Hi Gimbal2,

    I agree that threading is a complicated thing. But what is the solution in your eyes?
    - We could provide the code that solves the requirements. But that way nothing will be really explained.
    - We could try to describe everything here. But with such complex things, a forum is not really the correct way to go. In the past (with .Net development) I sometimes wrote a blog entry where I described something and then pointed in the forums to it. But that is simply not required here because there is a lot of documentation on the net already.

    This is a generic thing I also encountered in other forums: Often you simply cannot know for sure, what help someone really needs. When information is missing, you can guess sometimes. Or you can simply start asking the person about details. In generic points I like to point to locations where more information about that generic topic could be found. I am aware that this is not a perfect answer but right now I do not have a better way to help. (Maybe you think that it is not worth writing such a reply because I do not really add soemthing new. People know google. But often they simply didn't use it. And I do not just point to google but also give links what I found....)

    I am really interested in your point of view. Maybe you can help me writing better replies in the future. (Because that is the reason why I am replying in this thread. So if you could give any hints how this could be done better: you are welcome!)

    With kind regards,

    Konrad

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Java Input/Output program using threads

    That's an off-topic discussion here though; I don't want to hijack the thread of the OP. I would advise you to look at Jos's reply and try to figure out what the intention is behind that post and why he did not choose to link to tutorials about code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java Input/Output program using threads

    I read Jos's reply and found that a link to such a turoial might be helpfull. So sorry, I simply do not get your point right now.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Input/Output program using threads

    Quote Originally Posted by kneitzel View Post
    I read Jos's reply and found that a link to such a turoial might be helpfull. So sorry, I simply do not get your point right now.
    I assumed a minimal knowedge about Threads and a bit of OO knowledge, i.e. a simple class implementing the Runnable interface:

    Java Code:
    public class Cheer implements Runnable {
       private String cheer;
       private int n;
       public Cheer(String cheer, int n) { ... }
       public void run() { ... }
    }
    If the OP doesn't understand that, the OP has no business fiddling with Threads; all imho of course.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Output for the following Java Program
    By dhirajb1989 in forum New To Java
    Replies: 2
    Last Post: 09-09-2013, 05:17 AM
  2. Need Help With Simple Input Output Program
    By Mastermime in forum New To Java
    Replies: 12
    Last Post: 12-18-2012, 03:49 AM
  3. Input/Output without java.io
    By Arka in forum New To Java
    Replies: 2
    Last Post: 07-18-2012, 09:32 AM
  4. Need help with input/output program error
    By stefan2892 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 07:57 PM
  5. Replies: 4
    Last Post: 10-02-2008, 10:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •