xsnippet logotype

xsnippet

#362686, Untitled [ Text only ]

by Guest
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- questions from files / notes that I shared before
- "Thinking in Java" book examples


1. Example of publish-subscriber (multiple threads) using a queue.

2. Job Manager.
Implement console application that allows to 
- run a job
- stop a job
- get job status by job ID (Scheduled, Running, Finished, Failed)
- stop all jobs
- exit application

Usage example:
java -jar <app>.jar --max-threads=4
<app started to listen user's input>
>> start <job_type>
<< id=23

>> status 23
<< status=Running

>> stop 23
<< stopped
<< <list of statuses of current jobs>

>> stop-all
<< stopped

<job_type> - add different jobs to emulate real application:
- a job that fails with exceptions
- long-running job (1-2 min):
- infinite job ( while { //... } )

When you exit application (or executor pool) you have to list statuses of current jobs


3. Thread-safe, lazy singleton (no enums etc.).

4. Thread-safe Service Factory.
Factory below is responsible for initializing services, by their name. Initialization of single service takes considerable amount of time (1 minute). Number of services is limited with approximately 10 services, but factory will be called often (100 requests per second). 
Factory is working in multithreaded environment. 

public class ServiceFactoryImpl<T extends Service> {
     
     // ...
     
     // should be thread-safe
     // miminal blocking (for example, getService("a") shouldn't block getService("b))
     // lazy 
     public T getService(String name) {
     	// ...
     	// use createService method
     }
  
     private T createService(String name) {
        return new Service(name);
     }
}

class Service {
	// ...
}