Skip to content

Instantly share code, notes, and snippets.

@damithadayananda
damithadayananda / MathClass.java
Last active December 29, 2018 06:43
show the usage of synchronized block in java
package synchronize;
public class MathClass {
synchronized void printNumbers(int n) throws InterruptedException
{
for (int i=1; i<=n;i++){
System.out.println(Thread.currentThread().getName() + "::" + i);
Thread.sleep(500);
}
}
@damithadayananda
damithadayananda / main.go
Created December 29, 2018 08:11
Sieve of Eratosthenes algorithm implementation Golang
package main
import (
"runtime"
"fmt"
)
func main() {
nCPU:=runtime.NumCPU()
runtime.GOMAXPROCS(nCPU)
public static void main(string[] args){
new FixedThreadPoolExecutor();
}
package taskExecutor;
import java.util.concurrent.TimeUnit;
public class Task implements Runnable {
private String name;
public Task(String name){
this.name = name;
}
package taskExecutor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class FixedThreadPoolExecutor {
public FixedThreadPoolExecutor(){
ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(4);
for(int i=0; i<10;i++){
Task task = new Task("Task" + i);
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.FIELD) 
public @interface myField { 
public boolean enabled() default true;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface myMethod {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface myType {
public String brokers() default "localhost";
public boolean _continue() default false;
}
public class threadWithTheaClass extends Thread {
public void run(){
System.out.println("thread using class");
}
}
public class main{
public static void main(string[] args)
public class annonymousClassUsingThreadClass {
public annonymousClassUsingThreadClass() {
Thread t = new Thread(){
public void run(){
System.out.println("anonymous thread");
}
};
t.start();
}
}