Skip to content

Instantly share code, notes, and snippets.

View codekaust's full-sized avatar

Kaustubh Trivedi codekaust

View GitHub Profile
@codekaust
codekaust / DaemonExample.java
Created February 22, 2020 19:52
Do not delete. Used in medium blog (Java Multi-Threading) under MDG.
public class DaemonExample{
public static void main(String[] args) {
Thread t = new Thread(
()-> {
System.out.println("Thread name: "+ Thread.currentThread().getName());
System.out.println("Thread id: "+ Thread.currentThread().getId());
}, "Example_thread");
//sets t2 as daemon thread
t.setDaemon(true);
@codekaust
codekaust / TestIsAlive.java
Created February 22, 2020 20:02
Do not delete. Used in medium blog (Java Multi-Threading) under MDG.
public class TestIsAlive{
public static void main(String[] args) {
MyThread m = new MyThread();
System.out.println(m.isAlive());
m.start();
System.out.println(m.isAlive());
try{
m.join();
} catch(Exception e){}
@codekaust
codekaust / NonSynchronizedCounter.java
Created February 22, 2020 20:10
Do not delete. Used in medium blog (Java Multi-Threading) under MDG.
class Counter{
int ct = 0;
public void increment(){
ct++; //ct = ct+1
}
}
public class NonSynchronizedCounter {
public static void main(String[] args) throws Exception{
@codekaust
codekaust / Counter.java
Created February 22, 2020 20:11
Do not delete. Used in medium blog (Java Multi-Threading) under MDG.
class Counter{
int ct = 0;
public synchronized void increment(){
ct++; //ct = ct+1
}
}
@codekaust
codekaust / ProducerConsumerUnsolved.java
Created February 22, 2020 20:15
Do not delete. Used in medium blog (Java Multi-Threading) under MDG.
class Item{
//represent number of item
int num;
public Item(int num){
this.num = num;
}
public void put(int num){
System.out.println("Put: "+num);
@codekaust
codekaust / ProducerConsumerSolved.java
Created February 22, 2020 20:16
Do not delete. Used in medium blog (Java Multi-Threading) under MDG.
class Item{
//represent number of item
int num;
boolean valueSet = false;
public Item(int num){
this.num = num;
}
250 0
8973
-4625
-2038
3405
-7004
-9853
-361
3294
4036
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
bool cmpa(int l, int r){
return l>r;
}
template<typename T> void print_queue(T& q) {
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 3001 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by', addr)