Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
708 B
4
Indexable
Never
ExecutorService service = Executors.newFixedThreadPool(numThreads);
		for (int i=0; i<numLos; ++i) {
			service.submit(new Runnable() {
				int i;
				public Runnable init(final int i) {
					this.i = i;
					return this;
				}

				@Override
				public void run() {
					try {
						// CODE TO RUN IN PARALLEL

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.init(i));
		}

		// no more Runnables are accepted for submission, so shutdown the adding service
		// and start execution
		service.shutdown();

		// wait until all threads are done
		try {
			service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}