Skip to content

Instantly share code, notes, and snippets.

@harry1989
Last active April 7, 2016 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harry1989/309e9514326cafe3c1d5d185c24267c3 to your computer and use it in GitHub Desktop.
Save harry1989/309e9514326cafe3c1d5d185c24267c3 to your computer and use it in GitHub Desktop.
This program demonstrate how we can merge two channels alternatively
package main
import (
"fmt"
"sync")
func printEvenNumbers(ch chan int){
for i:=2; i<=45; i=i+2 {
ch<-i;
}
close(ch);
}
func printOddNumbers(ch chan int){
for i:=1; i<=25; i=i+2 {
ch<-i;
}
close(ch);
}
func combine(inputs... chan int) chan int {
var output = make(chan int)
var group sync.WaitGroup
for _, i := range inputs {
group.Add(1)
go func(input chan int) {
for {
x,y := <-input
output <- x
if y == false {
break;
}
}
group.Done()
} (i)
}
go func() {
group.Wait()
close(output)
} ()
return output
}
func merge_alternate(inputs... chan int) chan int {
var output = make(chan int)
var group sync.WaitGroup
group.Add(len(inputs))
go func() {
var total_ch = len(inputs)
total_cha := make(map[chan int]int)
/**
* For loops can be optimized here
*/
for {
for j,i:= range inputs {
var channel_done = total_cha[i]
//fmt.Println(channel_done)
if channel_done > 0 {
continue;
}
x,y:= <-i
//fmt.Println(x,y)
if y == false {
fmt.Println("Channel", i, j," closed", total_cha[i])
total_cha[i] = 1
group.Done()
total_ch =-1
//total_cha[j] = true
} else {
output<- x
}
}
if (total_ch == 0) {
break;
}
}
}()
go func() {
group.Wait()
close(output)
} ()
return output
}
func main() {
var a = make(chan int);
var b = make(chan int);
go printOddNumbers(a);
go printEvenNumbers(b);
//var c = make(chan int)
//var c = combine(a, b)
var c = merge_alternate(a, b)
//multiplex(a,b,c)
for i:= range c {
fmt.Println(i);
}
}
mport java.util.List;
import java.util.ArrayList;
public class Driver {
static Object lock = new Object();
static List<Integer> numbers = new ArrayList<>();
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
Thread t1 = new Thread(new Counter(numbers, false));
Thread t2 = new Thread(new Counter(numbers, true));
try {
t1.start();
t2.start();
t1.join();
//t2.join();
System.out.println(numbers);
System.out.println("\nPrinting over");
} catch (Exception e) {
System.exit(1);
}
}
}
class Counter extends Thread {
private static List<Integer> numbers;
private boolean even;
public Counter(List<Integer> num, boolean iseven) {
this.numbers = num;
this.even = iseven;
}
public void run(){
int itr = 1;
if(this.even){
itr = 2;
}
System.out.println("Starting " + itr);
for (; itr < 53; itr = itr + 2) {
//System.out.println(itr);
System.out.println("Waiting.." + itr);
synchronized (numbers) {
//numbers.wait();
//numbers.add(itr);
try {
numbers.wait();
numbers.add(itr);
numbers.notify();
//numbers.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Done in run");
}
}
public class Driver {
static Object lock = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
for (int itr = 1; itr < 51; itr = itr + 2) {
synchronized (lock) {
System.out.print(" " + itr);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
for (int itr = 2; itr < 51; itr = itr + 2) {
synchronized (lock) {
System.out.print(" " + itr);
try {
lock.notify();
if(itr==50)
break;
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("\nPrinting over");
} catch (Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment