About
--------sum of prime numbers -----------
public class summ implements Runnable{ private long start; private long end; public long summation;
public summ(long start,long end){
this.start=start;
this.end=end;
}
public void run(){
long summation=0;
for(long i=start;i<=end;i++){
int num=0;
for(int j=2;j<i/2;j++){
if(i%j==0){
num=1;
}
}
if(num==0){
summation=summation+i;
}
}
this.summation= summation;
}
}
public class Main { public static void main(String[] args){
long num=100;
long start=0;
long end=(int) num/4;
long temp=(int) num/4;
summ s1=new summ(start,end);
start=end+1;
end=start+temp;
summ s2=new summ(start,end);
start=end+1;
end=temp+start;
summ s3=new summ(start,end);
start=end+1;
end=temp+start;
summ s4=new summ(start,end);
Thread t1=new Thread(s1);
Thread t2=new Thread(s2);
Thread t3=new Thread(s3);
Thread t4=new Thread(s4);
t1.start();
t2.start();
t3.start();
t4.start();
try{
t1.join();
}catch(InterruptedException e){
System.out.print("error");
}
try{
t2.join();
}catch(InterruptedException e){
System.out.print("error");
}
try{
t3.join();
}catch(InterruptedException e){
System.out.print("error");
}
try{
t4.join();
}catch(InterruptedException e){
System.out.print("error");
}
long summation=s1.summation+s2.summation+s3.summation+s4.summation;
System.out.print("sum of prime numbers: ");
System.out.print(summation);
}
}
---------------------bank-------------------
import java.util.concurrent.*; class jointacc{ private int balance; public jointacc(int balance){ this.balance=balance; } public synchronized void deposit(int x){ this.balance=balance+x; } public synchronized void enquire(String name){ System.out.println(this.balance + name); } }
class cust implements Runnable{ private jointacc acc; private String name; private int depo; public cust(String name,int depo,jointacc acc){ this.name=name; this.depo=depo; this.acc=acc; } public void run(){ acc.deposit(this.depo); acc.enquire(this.name); }
}
class bank{ public static void main(String[] args){ jointacc acc = new jointacc(100); cust c1=new cust("yash",100,acc); cust c2=new cust("sash",200,acc); cust c3=new cust("hash",300,acc);
// ExecutorService executor=Executors.newCachedThreadPool();
ExecutorService executor=Executors.newFixedThreadPool(4);
executor.execute(c1);
executor.execute(c2);
executor.execute(c3);
executor.shutdown();
}
}
---------------------sum of 100 numbers----------------
class summ implements Runnable{ private long start; private long end; public long summation;
public summ(long start,long end){
this.start=start;
this.end=end;
}
public void run(){
long summation=0;
for(long i=start;i<=end;i++){
summation=summation+i;
}
this.summation= summation;
}
}
public class Mainn { public static void main(String[] args){
summ s1=new summ(0,25);
summ s2=new summ(26,50);
summ s3=new summ(51,75);
summ s4=new summ(76,100);
Thread t1=new Thread(s1);
Thread t2=new Thread(s2);
Thread t3=new Thread(s3);
Thread t4=new Thread(s4);
t1.start();
t2.start();
t3.start();
t4.start();
try{
t1.join();
}catch(InterruptedException e){
System.out.print("error");
}
try{
t2.join();
}catch(InterruptedException e){
System.out.print("error");
}
try{
t3.join();
}catch(InterruptedException e){
System.out.print("error");
}
try{
t4.join();
}catch(InterruptedException e){
System.out.print("error");
}
long summation=s1.summation+s2.summation+s3.summation+s4.summation;
System.out.print("sum of prime numbers: ");
System.out.print(summation);
}
}