Skip to content

Instantly share code, notes, and snippets.

View Yatharth0045's full-sized avatar
🎯
Focusing

Yatharth Sharma Yatharth0045

🎯
Focusing
View GitHub Profile
$ git config --global user.name <Your name>
@Yatharth0045
Yatharth0045 / Test.java
Created March 2, 2019 14:22
Its giving an error because of mismatch return type.
interface Interface {
int add(int x,int y);
}
class MethodReference {
void sum(int a,int b){
System.out.println(a+b);
//return a+b;
}
public static void main(String[] args) {
Interface i = new MethodReference()::sum;
@Yatharth0045
Yatharth0045 / Functional-Interface.java
Created March 5, 2019 12:44
Basic Functional Interface Syntax
@FunctionalInterface
interface FuncInterface{
void abstractMethod();
default void defaultMethod(){
//optional default method with some default implementation
}
static void staticMethod(){
//optional static method with some implementation
}
}
@FunctionalInterface
interface FuncInt1{
void abstractMethod();
}
@FunctionalInterface
interface Funcint2 extends FuncInt1{
}
@FunctionalInterface
interface FuncInt1{
void abstractMethod(String var);
}
@FunctionalInterface
interface Funcint2 extends FuncInt1{
void abstractMethod(String var);
}
@Yatharth0045
Yatharth0045 / Predefined-Interfaces-Example.java
Last active March 8, 2019 12:40
A program for printing the list of students with their roll number, name, marks and grade using predefined functional interfaces.
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
class Student {
//roll number assigner used by supplier
static int rollNoAssigner;
@Yatharth0045
Yatharth0045 / MyThread.java
Last active March 17, 2019 14:24
Implementation of Thread by overriding the run method
class MyThread {
public static void main(String[] args) {
for (int count = 0; count < 10; count++) {
Thread thread = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ": Child Thread");
});
thread.start();
}
for (int count = 0; count < 10; count++) {
System.out.println(Thread.currentThread().getName() + ": Main Thread " + count);
@Yatharth0045
Yatharth0045 / Parent-Kill-Child.java
Last active March 17, 2019 14:28
This programs signifies that the child threads dies if parent thread dies.
class MyThread {
public static void main(String[] args) {
for (int count = 0; count < 10; count++) {
Thread thread = new Thread(()->{
System.out.println("Thread "+Thread.currentThread().getName()+" is going to start");
try{
Thread.sleep(500);
} catch(InterruptedException exception){
exception.getMessage();
}
@Yatharth0045
Yatharth0045 / Executor-Thread-Pool.java
Last active March 26, 2019 13:13
This program shows the execution of ExecutorService class.
class Threads {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
for(int count = 0; count < 10; count++){
service.execute(()->{
System.out.println("Child Thread" + Thread.currentThread().getName());
});
}
service.shutdown();
System.out.println("|Main Thread|");
@Yatharth0045
Yatharth0045 / RunAsyncExample.java
Created April 3, 2019 01:22
An example to show runAsync method of completablefuture.
import java.util.concurrent.CompletableFuture;
class Test {
public static void main(String[] args) {
CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Runnable Call to be executed by another thread");
});
System.out.println(Thread.currentThread().getName() + " Main Thread");
}
}