Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- First(new Thread() {
- public void run() {
- //do something
- }
- });
- public class FirstCaller {
- private void method1() { }
- private void method2() { }
- private void method3() { }
- public void someMethod() {
- First(new Thread() {
- public void run() {
- //do something
- method1();
- method2();
- method3();
- }
- });
- }
- }
- public class FirstCaller {
- public void someMethod() {
- new First(new Thread() {
- private void method1() { }
- private void method2() { }
- private void method3() { }
- public void run() {
- //do something
- method1();
- method2();
- method3();
- }
- });
- }
- }
- ExecutorService service = Executors.newSingleThreadedPool();
- service.submit(runnable1);
- service.submit(runnable2);
- service.submit(runnable3);
- public class MultiRunnable implements Runnable {
- private Runnable runnable1;
- private Runnable runnable2;
- public MultiRunnable(Runnable runnable1, Runnable runnable2) {
- this.runnable1 = runnable1;
- this.runnable2 = runnable2;
- }
- @Override
- public void run() {
- runnable1.run();
- runnable2.run();
- }
- }
- import java.util.Arrays;
- import java.util.List;
- public class MultiRunnable implements Runnable {
- private List<Runnable> runnables;
- public MultiRunnable(Runnable... runnables) {
- this.runnables = Arrays.asList(runnables);
- }
- public MultiRunnable(List<Runnable> runnables) {
- this.runnables = runnables;
- }
- @Override
- public void run() {
- for(Runnable runnable : runnables)
- runnable.run();
- }
- }
- class MyThread extends Thread {
- public enum Action { A, B, C }
- private Action mAction;
- public void run() {
- if (mAction == null) {
- throw new IllegalStateException("Action must be specified");
- }
- switch (mAction) {
- case A:
- methodA();
- break;
- case B:
- methodB();
- break;
- case C:
- methodC();
- break;
- }
- }
- public void setAction(Action action) {
- if (action == null) {
- throw new IllegalArgumentException("Action cannot be null");
- }
- mAction = action;
- }
- private void methodA() { ... }
- private void methodB() { ... }
- private void methodC() { ... }
- }
- private EecutorService threadPool = Executors.newSingleThreadedPool();
- ...
- threadPool.submit(new First());
- threadPool.submit(new Second());
- threadPool.submit(new Third());
- ...
- // when you are done submitting, always shutdown your pool
- threadPool.shutdown();
Add Comment
Please, Sign In to add comment