Name |
|
---|---|
Backends | JVM |
Maven coordinates | |
Compatible Ceylon release |
JVM: 1.2.x, 1.3.x (latest) |
Published | Jan 19, 2019 |
Stats |
Downloads (JVM): 36 Source downloads: 48 |
Description |
This module contains simple [[Pool]] for keeping different reusable objects. Abstract [[PooledResource]] class should be extend by real resource to be kept. [[ResourceFactory]] interface should be satisfied with object that creates your resources for your pool. Example to use (also can be found in the package [[com.github.kopilov.abstractpool.example]]: FactoryExample.ceylon import com.github.kopilov.abstractpool { ResourceFactory } class FactoryExample() satisfies ResourceFactory<ResourceExample> { shared actual ResourceExample createResource() { print ("createResource"); return ResourceExample(); } } ResourceExample.ceylon import com.github.kopilov.abstractpool { PooledResource } class ResourceExample() extends PooledResource() { variable String d = ""; shared actual void obtain() { d = d + "obtained "; super.obtain(); } shared actual void release(Throwable? error) { d = ""; super.release(error); } shared void calculateAndPrint(Integer a, Integer id) { for (i in 1..a) { for (j in 1..a) { for (k in 1..a) { if (i == a && j == a && k == a) { print("Everything is ``a``"); print("i = ``id``, d = ``d``"); } } } } } shared actual void close() {} } test.ceylon import com.github.kopilov.abstractpool { Pool } import java.util.concurrent { ExecutorService, Executors } import java.lang { Thread } shared void run() { FactoryExample fe = FactoryExample(); Float expirationTime = 5.0; value pool = Pool<ResourceExample>(fe, expirationTime); ExecutorService executor = Executors.newFixedThreadPool(8); for (i in 1..5000) { void task() { try (r = pool.getResource()) { print("getResource"); r.calculateAndPrint(10, i); } } executor.execute(task); } print("size 1 = ``pool.size``"); executor.shutdown(); while(!executor.terminated) { Thread.sleep(10); } print("size 2 = ``pool.size``"); Thread.sleep(10 * 1000); //pause 1 print("size 3 = ``pool.size``"); try (r = pool.getResource()) { print("getResource"); r.calculateAndPrint(10, 0); } Thread.sleep(1000); //pause 2 print("size 4 = ``pool.size``"); } Some important things that we can notice in the output:
|
Dependencies |
ceylon.time/1.3.3
shared
java.base/8 JDK
|
Usage |
import com.github.kopilov.abstractpool "1.1.0"; |
Module links |
Members Imported By Browse Download .car No .js archive Download source archive No module documentation No API documentation |