Refactor core-java (#2324)

This commit is contained in:
Grzegorz Piwowarek 2017-07-28 18:15:37 +02:00 committed by GitHub
parent d71f330b09
commit 9b143b820c
4 changed files with 29 additions and 33 deletions

View File

@ -4,22 +4,20 @@ import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample {
public void start() {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
// Task
System.out.println("All previous tasks are completed");
});
public void start() {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
// Task
System.out.println("All previous tasks are completed");
});
Thread t1 = new Thread(new Task(cyclicBarrier), "T1");
Thread t2 = new Thread(new Task(cyclicBarrier), "T2");
Thread t3 = new Thread(new Task(cyclicBarrier), "T3");
if (!cyclicBarrier.isBroken()) {
t1.start();
t2.start();
t3.start();
}
}
Thread t1 = new Thread(new Task(cyclicBarrier), "T1");
Thread t2 = new Thread(new Task(cyclicBarrier), "T2");
Thread t3 = new Thread(new Task(cyclicBarrier), "T3");
if (!cyclicBarrier.isBroken()) {
t1.start();
t2.start();
t3.start();
}
}
}

View File

@ -6,20 +6,19 @@ import java.util.concurrent.CyclicBarrier;
public class Task implements Runnable {
private CyclicBarrier barrier;
public Task(CyclicBarrier barrier) {
this.barrier = barrier;
}
@Override
public void run() {
public Task(CyclicBarrier barrier) {
this.barrier = barrier;
}
@Override
public void run() {
try {
System.out.println("Thread : "+ Thread.currentThread().getName() + " is waiting");
barrier.await();
System.out.println("Thread : "+ Thread.currentThread().getName() + " is released");
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
System.out.println("Thread : " + Thread.currentThread().getName() + " is waiting");
barrier.await();
System.out.println("Thread : " + Thread.currentThread().getName() + " is released");
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
}

View File

@ -4,7 +4,7 @@ public class Rectangle extends Shape {
private double width;
private double length;
public Rectangle(double width, double length) {
Rectangle(double width, double length) {
this.width = width;
this.length = length;
}

View File

@ -1,11 +1,10 @@
package org.baeldung.executable;
import javax.swing.JOptionPane;
import javax.swing.*;
public class ExecutableMavenJar {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "It worked!", "Executable Jar with Maven", 1);
}
}