----------------------------------------------------sum of prime no---------------------------------------------------------
package prac3;
public class threadexample {
private static final int LIMIT = 10_000_000;
private static final int NUM_THREADS = 4;
public static void main(String[] args) throws InterruptedException {
PrimeThread[] threads = new PrimeThread[NUM_THREADS];
int segmentSize = LIMIT / NUM_THREADS;
int start = 2;
int end = segmentSize;
for (int i = 0; i < NUM_THREADS; i++) {
threads[i] = new PrimeThread(start, end);
start = end + 1;
end += segmentSize;
}
for (PrimeThread thread : threads) {
thread.start();
}
for (PrimeThread thread : threads) {
thread.join();
}
long sum = 0;
for (PrimeThread thread : threads) {
sum += thread.getSum();
}
System.out.println("Sum of prime numbers up to " + LIMIT + ": " + sum);
}
}
class PrimeThread extends Thread {
private final int start;
private final int end;
private long sum;
PrimeThread(int start, int end) {
this.start = start;
this.end = end;
this.sum = 0;
}
public long getSum() {
return sum;
}
public void run() {
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
sum += i;
}
}
}
private boolean isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
-----------------------------------------------------account without syn---------------------------------------------
import java.util.concurrent.*;
public class AccountWithoutSync {
private static Account account = new Account();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool(); //create executor
// Create and launch 100 threads
for (int i = 0; i < 10; i++) {
executor.execute(new AddAPennyTask()); //submit task
}
executor.shutdown(); //shut down executor
// Wait until all tasks are finished
while (!executor.isTerminated()) { //wait for all tasks to //terminate
}
System.out.println("What is balance? " + account.getBalance());
}
// A thread for adding a penny to the account
private static class AddAPennyTask implements Runnable {
public void run() {
account.deposit(1);
}
}
// An inner class for account
private static class Account {
private int balance = 0;
public int getBalance() {
return balance;
}
public void deposit(int amount) {
int newBalance = balance + amount;
// This delay is deliberately added to magnify the
// data-corruption problem and make it easy to see.
try {
Thread.sleep(5);
}
catch (InterruptedException ex) {
}
balance = newBalance;
System.out.println("newBalance is:"+newBalance);
}
}
}
-----------------------------------------sum of 1 to 100 using 4 threads-----------------------------------------------------------------
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);
}
}
-------------------------------------------------joint acc by 3 people------------------------------------------
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();
}
}
object foldLeft {
def main(args: Array[String]): Unit = {
def foldLeft(list: List[Int], initial: Int, f: (Int, Int) => Int): Int = {
var acc = initial
for (element <- list) {
acc = f(acc, element)
}
acc
}
val numbers = List(1, 2, 3, 4, 5)
val sum = foldLeft(numbers, 0, (acc, elem) => acc + elem)
println(sum)
}
}
object mapp {
def main(args: Array[String]): Unit = {
def map[A, B](list: List[A], f: A => B): List[B] = {
list match {
case Nil => Nil
case head :: tail => f(head) :: map(tail, f)
}
}
val numbers = List(1, 2, 3, 4, 5)
val doubledNumbers = map(numbers, (x: Int) => x * 2)
println(doubledNumbers)
}
}
object Mapp{
def main(args: Array[String]): Unit = {
// var arr: Array[Int] = new Array[Int](5);
def maps(arr: Array[Int], x: Int, fn : (Array[Int], Int)=>Array[Int]) : Array[Int] = fn(arr, x)
var arr = Array(1, 2, 3, 4, 5);
var newarr = maps(arr, 5, (arr, x) => arr.map(_ + x))
println("Mapped Array: ");
newarr.foreach(println);
}
}
object redRight {
def main(args: Array[String]): Unit = {
def reduceRight[A](list: List[A])(f: (A, A) => A): A = {
list match {
case Nil => throw new UnsupportedOperationException("Empty list")
case head :: Nil => head
case head :: tail => f(head, reduceRight(tail)(f))
}
}
val numbers = List(1, 2, 3, 4, 5)
val result = reduceRight(numbers)((a, b) => a - b)
println(result)
}
}
object redLeft {
def main(args: Array[String]): Unit = {
def reduceLeft(list: List[Int], f: (Int, Int) => Int): Int = {
// list match {
// case Nil => throw new UnsupportedOperationException("Empty list")
// case head :: tail => tail.foldLeft(head)(f)
// }
var x = list(0)
var i = 1
while (i<list.size)
{
x = f(list(i),x)
i=i+1
}
x
}
val numbers = List(1, 2, 3, 4, 5)
val sum = reduceLeft(numbers, (a, b) => a + b)
println(sum)
}
}