About
Example to create a new thread and run it class ThreadDemo extends Thread { private Thread t; private String threadName;
ThreadDemo(String name) { threadName = name; System.out.println("Creating " + threadName ); }
public void run() { System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start () { System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
} }
public class TestThread1{
public static void main(String args[]) { ThreadDemo T1 = new ThreadDemo("Thread-1"); T1.start();
ThreadDemo T2 = new ThreadDemo("Thread-2");
T2.start();
}
}
class ThreadOps implements Runnable { public Thread t; private String threadName; boolean suspended = false;
ThreadOps(String name) { threadName = name; System.out.println("Creating " + threadName ); }
public void run() { System.out.println("Running " + threadName );
try {
for(int i = 10; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(300);
synchronized(this) {
while(suspended) {
wait();
}
}
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start () { System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
void suspend() { suspended = true; }
synchronized void resume() { suspended = false; notify(); } }
public class OpsTst {
public static void main(String args[]) { ThreadOps R1 = new ThreadOps("Thread-1"); R1.start();
ThreadOps R2 = new ThreadOps("Thread-2");
R2.start();
try {
Thread.sleep(1000);
R1.suspend();
System.out.println("Suspending First Thread");
Thread.sleep(1000);
R1.resume();
System.out.println("Resuming First Thread");
R2.suspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
R2.resume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
} try {
System.out.println("Waiting for threads to finish.");
R1.t.join();
R2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
} }
scala
higher1
{
def math(x: Double, y:Double, fn: (Double,Double)=>Double): Double = fn(x,y);
def main(args:Array[String])
{
val result = math(50,20,(x,y)=>x max y);
println(result);
}
}
object higher2
{
def math(x: Double, y:Double, z:Double, fn: (Double,Double)=>Double): Double = fn(fn(x,y),z);
def main(args:Array[String])
{
val result = math(50,20,10,(x,y)=>x * y);
println(result);
}
}
object curry { def add(x:Int,y:Int) = x + y def add2(x:Int) = (y:Int) => x + y; //curried version def add3(x:Int)(y:Int) = x + y; //Simpler scala version def main(args:Array[String]) { println(add(20,10)); println(add2(20)(10)); //calling curried add2 val sum40 = add2(40); println(sum40(100)); //Partial application println(add3(100)(200)); //calling simpler scala version val sum50 = add3(50)_; //partial application of simple version println(sum50(400)); } }
object myreduce {
val lst = List(1,2,3,5,7,10,13);
val lst2 = List("A","B","C");
def main(args:Array[String])
{
println(lst.reduceLeft(_+_)); //adds all the elements of lst
println(lst.reduceLeft((x,y)=>{println(x + "," + y);x+y;}))
println(lst2.reduceLeft(_+_)); //concatenates lst2
println(lst.reduceRight(_+_)); //adds all the elements of lst
println(lst.reduceLeft(_-_)); //subtract using reduceRight
println(lst.reduceRight(_-_)); //subtract using reduceRight
println(lst.reduceLeft((x,y)=>{println(x + "," + y);x-y;}))
println(lst.reduceRight((x,y)=>{println(x + "," + y);x-y;}))
}
}