Close

Results 1 to 5 of 5
  1. #1
    DF VIP Member OrangeJuicey's Avatar
    Join Date
    Aug 2006
    Location
    London
    Posts
    1,283
    Thanks
    1
    Thanked:        4
    Karma Level
    289

    Help Java Thread Pools and System.in

    First off if I was going to take input from System.in using a scanner, and I only wanted it to accept the commands cmd:selectItem, cmd:listItem.
    Would i use a case statement which then has a default clause throw new exception e. which would be thrown if a command that wasn't recognized was entered.

    Secondly if i wanted to implement more commands to work on my Collection<items> such as countItems, this would then lead to a thread being created which will carry out the activity in the background, can someone tell me how to create the threads concurrecntly e.g thread pool?
    For example if i wanted to findITEMname. And status was an array in item that contained several names, how would I get round the problem if multiple calls of findITEMname were called.

    Any help is greatly appreciated. Peace.

  2. #2
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: Java Thread Pools and System.in

    Hi, java does not allow you to use Strings within a switch statement. Only primitives types (String is an object. )

    As from your other thread, use compareTo (have to be in an if statement block) or, as I said on the other reply, use integers representing the choices and then you can also use switch. (make comparisons a hell of a lot easier too!)

    Code:
    switch (option) {
      case 1: 
        /*whatever*/
      case 2:
        /*whatever*/
      case 3:
        /*whatever*/
      default:
        throw whateverException();
    }
    To create threads, you can either extend thread (do only if you don't need to extend another class) or implement runnable. You then need to implement run() and start() methods (there may be more abstract ones, I cannot remember, lol). Calling start() will call the run() method.

    Not too sure about the multiple calls. I assume each thread would take it's own copy of the array and would run ok (check this!)

  3. #3
    DF VIP Member OrangeJuicey's Avatar
    Join Date
    Aug 2006
    Location
    London
    Posts
    1,283
    Thanks
    1
    Thanked:        4
    Karma Level
    289

    Default Re: Java Thread Pools and System.in

    thanks mate, I think what it needs is a lock so that the 2nd thread only starts running when 1st thread releases the lock .notify and .notifyall commands. Thanks for the help mate.

  4. #4
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    546

    Default Re: Java Thread Pools and System.in

    as John says you can't use a string but you can split the first character off and use that - as long as each choice you are trying to use has a different first character (or second etc).

  5. #5
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: Java Thread Pools and System.in

    or use the hashcode.

Similar Threads

  1. DF Charity Thread **Updated**
    By biggy7 in forum Cheapskates Corner
    Replies: 50
    Last Post: 16th January 2005, 02:06 AM
  2. System Crashes During Burning
    By djc53320661 in forum PC Problems
    Replies: 13
    Last Post: 1st December 2003, 12:13 PM
  3. Yet another copy protection system
    By sanjuro in forum PC Hardware
    Replies: 2
    Last Post: 30th August 2002, 12:50 PM

Social Networking Bookmarks

Posting Permissions

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