Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

An expert to take a look at my java program

$
0
0
I need help to make Each thread will first create an array (size = 30) and populate it with a sequence of numbers ??
I need to create three threads which i showed here and trying to connect in some ways

After the thread has completely filled the array the thread will display the value of the last element.??

program

import java.io.IOException;
import java.util.Scanner;

public class MultiThread{                                   //fibonacci     
      PerfectSquare thread2 = new PerfectSquare();
      Fibonacci thread3 = new Fibonacci();
      ThreadDemo thread4 = new ThreadDemo();
      ExecuteThread thread1 = new ExecuteThread();
}



Perfect Square
import java.io.IOException;
public class PerfectSquare extends Thread{
   public static void main(String[] args) throws IOException{
       int starting_number = 1;
       int ending_number = 100;
       System.out.println("Perfect Numbers between"+starting_number+ " and "+ending_number);
       for (int i = starting_number; i <=ending_number; i ++){
           int number = i;
           int sqrt = (int) Math.sqrt(number);
           if(sqrt * sqrt == number){
               System.out.println(number+" = "+sqrt+"*"+sqrt);
            }
    }
}
}



FIbonacci
public class Fibonacci extends Thread{
public static void main(){
System.out.println("Fibonacci Sequences are: "); // statement
fibonacciPrint(8); // amount of integer value to be displayed to user.
}

public static final void fibonacciPrint(long n){ //interger is pass as parameter for method fiboprint
int a0 = 1; //both integers is passed to variable n that has long data type
int a1 = 1;


//you need a run method for the calculation to start 
for(int i = 0; i < 7; i++){  //for loop that increase value by 1 each time
System.out.println(a0 + " ");
final int secondValue = a1; //save value a1
a1 = a1 + a0; //both values added together
a0 = secondValue;
}
}

//need to create a private void run method for other thread
//to use
}



public class ExecuteThread extends Thread{
int sleepTime; //variable need to be assign

public ExecuteThread (String name){ //the names will all display at once //method start runs the program
super(name);
start();

}

ExecuteThread()
{
}

public void run(){
//sleep for zero to five seconds to display data piece by piece
sleepTime = (int) (Math.random() * 5000);
//multiply it to a value
System.out.println (getName() + " is working for " + sleepTime + "micro seconds");
try{ //throws an error needs to be in a try and catch block
sleep(sleepTime);
} catch(InterruptedException e){
System.err.println("Exception: " + e.toString());
}
//output name that are in string form
System.out.println("Thread " + getName());
}
}



The three sequences are:
Fibonacci Sequence: 8, ...
Perfect Squares: 25, ...
2^n: 64, ...

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>