0

I am a beginner in programming. I am still learning about threads and more of them. But now I have quite a big idea to write my first program (I mean bigger than simple calculator). I want it to sort files, integrate in one (many copies of one file in different localization - the idea of it is of no importance now).

But I want to create threads, or anything else (what is your advice). I mean. When I start the program, the console starts up, and e.g I have to write "my_programm run" or "my_program stop" or "my_program status" or "my_magic_tricks be_done". I mean how can i create a program working in the background like in threads with real time string control over it. I tried to find out on Google for anything which could be useful for me, but i didn't find it out.

Please give me just a name of libraries or methods, which I can use. I will read out, what it is about and finally I will move forward with it.

If it is a dumbass question. I am really sorry for disapointing the programmer group. But it would be nice to be given of any signpost or clue.

xross
  • 597
  • 4
  • 9
  • 25
  • 1
    Welcome to Stack Overflow. I know, it can be overwhelming. Can you break this question into smaller questions that are more _directly_ answerable? Be as clear and simple as possible. Here, it appears that you want to do too many things at once. – Kedar Mhaswade Apr 21 '16 at 21:59
  • Maybe you are right. What libraries should I use, which make real-time reading the command line. – xross Apr 21 '16 at 22:04
  • 1
    If you're a beginner, stay away from threads for now. There are a lot of more basic things to learn first that will stand you in better stead. – Andy Turner Apr 21 '16 at 22:05
  • I want my program to run in the background. It is possible, that it will be working for many hours. So, for example during that process I want it to shut down safely (I will write it manually, how he should exit). But how can I give him a singnal for stopping the thread in my safe-mode – xross Apr 21 '16 at 22:05
  • hope my questions are more clear and simple. – xross Apr 21 '16 at 22:06

2 Answers2

3

A simple way to do it using the standard library :

import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

public class Example {
    private static final int POOL_SIZE = 5;
    private static final ExecutorService WORKERS = new ThreadPoolExecutor(POOL_SIZE, POOL_SIZE, 1, MILLISECONDS, new LinkedBlockingDeque<>());

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("> ");
            String cmd = sc.nextLine();
            switch (cmd) {
                case "process":
                    WORKERS.submit(newExpensiveTask());
                    break;

                case "kill":
                    System.exit(0);

                default:
                    System.err.println("Unrecognized command: " + cmd);
            }
        }
    }

    private static Runnable newExpensiveTask() {
        return () -> {
            try {
                Thread.sleep(10000);
                System.out.println("Done processing");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
    }
}

This code lets you run heavy tasks asynchronously while the user terminal remains available and reactive.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • Whoa, didnt know it could be made in such an easy way. Thank you for this ;). I am sure I will use it in my idea. – xross Apr 21 '16 at 22:15
  • @xross no problem. If it helps, I'll be happy to get an upvote :D – Dici Apr 21 '16 at 22:19
  • I did it, but the upvote will be marked just after I will gain more reputation points :/. So be patient it will be voted ;). Thank you – xross Apr 21 '16 at 22:24
0

I would recommend reading up on specific tutorials, such as the Java Language Tutorial (available as a book - at least, it used to be - as well as on the Java website https://docs.oracle.com/javase/tutorial/essential/concurrency/)

However as others have cautioned, getting into threading is a challenge and requires good knowledge of the language quite apart from the aspects of multithreading and synchronization. I'd be tempted to recommend you read some of the other tutorials - working through IO and so on - first of all.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • In my opinion, I am able to do a good IO. I have made something like celluar automaton, which simulates cell live :). It wasn't so hard, I think so. Maybe it isnt so advanced but it was a good exercise – xross Apr 21 '16 at 22:11