Areo IT
Knowledge database
24 mar 2014
24 paź 2013
Thread pool : Callable, Runnable, Executors
package com.areo; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Main { public static void main(String[] args) { executorWithRunnable(); executorWithCallable(); } private static void executorWithCallable() { ExecutorService pool = Executors.newFixedThreadPool(10); ArrayList<Future<Integer>> list = new ArrayList<Future<Integer>>(); for (int i = 0; i < 300; i++) { Future<Integer> submit = pool.submit(new MyCallable()); list.add(submit); } //father results int result = 0; for (Future<Integer> future : list) { try { result += future.get(); } catch (InterruptedException e) { } catch (ExecutionException e) { } } //result is 300 System.out.println("Result: " + result); } private static void executorWithRunnable() { ExecutorService pool = Executors.newFixedThreadPool(10); for (int i = 0; i < 300; i++) { pool.execute(new MThread()); } pool.shutdown(); } //callable static class MyCallable implements Callable<Integer>{ public Integer call() throws Exception { System.out.println("Calling!"); return 1; } } //runnable static class MThread implements Runnable{ public void run() { long id = Thread.currentThread().getId(); System.out.println(String.format("Thread id = %d", id)); } } }
20 lis 2012
Guava - EventBus example
package com.areoit.guava.eventbus; import org.junit.Test; import static org.junit.Assert.assertEquals; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; public class EventBusIntegerEventsTests { private static final String EVENT_BUS_ID = "numbers"; /** * Case: * + one listener * + listener subcribe for Integer event * + listener subcribe for Number event * + REMEMBER: Integer IS-A Number */ @Test public void oneIntegerEventTwoSubscribingMethod() { IntegerListener listener = new IntegerListener(); Integer integerEvent = Integer.MAX_VALUE; EventBus eventBus = new EventBus(EVENT_BUS_ID); eventBus.register(listener); eventBus.post(integerEvent); //check event counters int integerCount = listener.getIntegerEventCount(); int numberCount = listener.getNumberEventCount(); assertEquals(1, integerCount); assertEquals(1, numberCount); } class IntegerListener { private int integerEventCount = 0; private int numberEventCount = 0; @Subscribe public void onIntegerEvent(Integer event) { ++integerEventCount; } @Subscribe public void onNumberEvent(Number event) { ++numberEventCount; } public int getIntegerEventCount() { return integerEventCount; } public int getNumberEventCount() { return numberEventCount; } } }
Guava - Functions + composition
public class FunctionsTests { @Test public void Functions_converter_example() { FunctionstringToInteger = new Function () { @Override public Integer apply(String input) { return Integer.valueOf(input); } }; Function integerToLong = new Function\ () { @Override public Long apply(Integer input) { return Long.valueOf(input.longValue()); } }; Function stringToLong = Functions.compose(integerToLong, stringToInteger); Long actual = stringToLong.apply("5"); Long expected = 5L; assertEquals(expected, actual); } }
19 lis 2012
Guava - Joiner
import org.junit.Test; import com.google.common.base.Joiner; public class Collections { @Test public void Joiner_on_list_with_one_null_value() { String actual = Joiner .on(",") .skipNulls() .join("one",null,"two"); assertEquals("one,two", actual); } @Test public void Joiner_on_map() { Mapmap = new HashMap (); map.put("k1", "v1"); map.put("k2", "v2"); String actual = Joiner .on(",") .withKeyValueSeparator(" => ") .join(map); assertEquals("k1 => v1,k2 => v2", actual); } }
14 lis 2010
Inner class
- Klasy inner nie mogą mieć czegokolwiek static!
- MyOuter mo = new MyOuter(); // gotta get an instance!
MyOuter.MyInner inner = mo.new MyInner(); - Instancja poza Outer class przez instancje.new Inner()
- IInstancja z klasy Outer Inner() normalnnie
- Nie mozna tworzyc Inner w statycznych metodach outer!!
- this w metodach Inner class odności sie do obiektu Inner !
- Outer.this odnosi sie wiadomo ( z poziomu metod Inner)
- Modyfikatory przy class Inner{} => public,protected,private ,final,abstract,static,strictfp //jak normalny member klasy Outer!!
- Local method class może być instance tylko w tej metodzie!
- Local method class może dobrać sie do Outer private members!
- Local method class nie może dostać sie do zmiennych w tej metodzie(nie fianl)!
- Local method class może dostać sie do zmiennych fonal w tej metodzie!!
- Local method class może być zonaczona jako final!! ( tak jak zmienna lokalna ,zasady)
- Local method class w metodzie statycznej ma tylko dostęp do zmiennych statycznych!! i nie ma dostępy do zmiennych outerClass!!
- Pamiętaj średniku po definicji klasy anonymous!!
- Klasa anonymous raczej overriding method niż dodaje ( problem z widzeniem nowej innej metody)
- interface cannot be in method :(
- Anonymous class moze implements tylko 1 interfejs!!
13 lis 2010
Generics
- List oznacza każdą listę czegokolwiek
- List foo = new ArrayList();// bad!
- 1) List list = new ArrayList
(); //OK
2) List aList = new ArrayList();//OK
3) List foo = new ArrayList(); //BAD
4) List cList = new ArrayList();//BAD
5) List bList = new ArrayList(); //OK
6) List dList = new ArrayList(); //BAD
Subskrybuj:
Posty (Atom)