Skip to content

Instantly share code, notes, and snippets.

View GorvGoyl's full-sized avatar

Gourav Goyal GorvGoyl

View GitHub Profile
@GorvGoyl
GorvGoyl / Events and Delegates.cs
Created July 31, 2015 10:55
Simplest Example possible on Events and Delegates in C#
using System;
public class Publisher //main publisher class which will invoke methods of all subscriber classes
{
public delegate void TickHandler(Publisher m, EventArgs e); //declaring a delegate
public TickHandler Tick; //creating an object of delegate
public EventArgs e = null; //set 2nd paramter empty
public void Start() //starting point of thread
{
while (true)
@GorvGoyl
GorvGoyl / ProdCon.java
Created July 12, 2015 21:25
Producer Consumer problem - Solution using Semaphore in Java
import java.util.concurrent.Semaphore;
class Queue {
int value;
static Semaphore semProd = new Semaphore(1); //Producer is first getting lock on producer semaphore
static Semaphore semCon = new Semaphore(0);
@GorvGoyl
GorvGoyl / ProducerConsumerService.java
Created July 12, 2015 21:08
Producer Consumer problem - Solution using BlockingQueue() in Java
import java.util.concurrent.*;
class Producer implements Runnable {
private BlockingQueue<Integer> queue;
Producer(BlockingQueue<Integer> q){
this.queue=q;
}
@GorvGoyl
GorvGoyl / InterThreadCommunicationExample.java
Created July 12, 2015 20:49
Producer Consumer problem - Solution using Wait() notify() in Java
import java.util.*;
public class InterThreadCommunicationExample {
public static void main(String args[]) {
final LinkedList<Integer> sharedQ = new LinkedList<Integer>();
Thread producer = new Producer(sharedQ);
Thread consumer = new Consumer(sharedQ);