public class Example {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
CounterThread ct1 = new CounterThread(counter);
ct1.start();
CounterThread ct2 = new CounterThread(counter);
ct2.start();
Thread.sleep(1000);
}
}
class Counter {
private long counter = 0L;
public void increaseCounter1() {
counter++;
}
public long getCounter() {
return counter;
}
}
class CounterThread extends Thread
{
private Counter counter;
public CounterThread(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
for(int i=0; i<10; i++) {
counter.increaseCounter1();
System.out.println("Thread#1 " + i);
}
}
}
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Main {
static class Task extends Thread {
private final Queue<Integer> queue;
public Task(Queue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
for (int i=0; i<=10; ++i) {
queue.add(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
final BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(100);
final BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(100);
final Thread thread1 = new Task(queue1);
thread1.start();
final Thread thread2 = new Task(queue2);
thread2.start();
while(true) {
System.out.println(queue1.take() + queue2.take());
}
}
}