0

I need to write two programs. 1 sequential (Done) and 1 parallel and I've done something but i don't know if I've made it parallel or not and i also need to know:

  1. If (Thread.State state = Thread.currentThread().getState();) is the code to display the status of a thread
  2. how to assign different threads to different processors (4 cores)
  3. How to display the status of the processor
  4. calculation for each processor
  5. how to generate error messeges(Memory Consistency Errors, etc.)

Following is my code:

class Threads implements Runnable {

    @Override
    public void run() {

        Thread t = Thread.currentThread();

        Thread.State state = Thread.currentThread().getState();
        String Assignment = "calculations in array";
        String name = "os.name";
        String version = "os.version";
        String architecture = "os.arch";
        String[] array = new String[1312500];
        int size = array.length;

        Random r = new Random();
        int[] values = new int[1312500];
        int sumarray = 0;
        int CountD = 0;

        for (int i = 0; i < values.length; i++) {
            int randomint = r.nextInt(100);

            values[i] = randomint;

            if ((values[i] % 2 != 0) && (values[i] >= 25 && values[i] <= 75)) {
                sumarray += values[i];
                CountD++;
            }
        }

        System.out.println(t.getName());
        System.out.println("Thread Id " + t.getId());
        System.out.println("Thread Priority " + t.getPriority());
        System.out.println("status " + state);
        System.out.println("OS Name: " + System.getProperty(name));
        System.out.println("OS Version: " + System.getProperty(version));
        System.out.println("OS Architechture: " + System.getProperty(architecture));
        System.out.println(Assignment);
        System.out.println("Size of the Array is: " + array.length);
        System.out.println("Total number of system cores(processors): " + Runtime.getRuntime().availableProcessors());
        System.out.println("Difference of Array and Processors 1312500/4 = "
                + array.length / Runtime.getRuntime().availableProcessors());
        System.out.println("The size of array is divisible by the number of processors");
        System.out.println("Summary is: " + sumarray);
        System.out.println("The Average is: " + (sumarray / CountD));
    }

}

Main class:

class Concurrent {

    public static void main(String[] args) {
        Thread t1 = new Thread(new Threads());
        t1.start();

        Thread t2 = new Thread(new Threads());
        t2.start();

        Thread t3 = new Thread(new Threads());
        t3.start();

        Thread t4 = new Thread(new Threads());
        t4.start();
    }
}
Gray
  • 115,027
  • 24
  • 293
  • 354
  • You need to google up a tutorial on Threads and Java and begin there. Good luck. – 7663233 Mar 24 '17 at 10:25
  • did alot of that but still confused and cant find anything even remotely related to my needs. – Moiz Farooqui Mar 24 '17 at 11:00
  • Hi and Welcome to Stackoverflow. Please read the [tourguide](http://stackoverflow.com/tour) to learn how to ask questions the right way and how stackoverflow works. – jsadev.net Mar 24 '17 at 11:26
  • is it too much to ask for a help i'm not telling someone else to do my work for me – Moiz Farooqui Mar 24 '17 at 11:42
  • i (maybe others too) try to imagine the rationale behind your question and can't help suggesting you to study about java, threads, operating systems, and CPUs – nandsito Mar 24 '17 at 13:02

2 Answers2

3

[I need to know] If Thread.State state = Thread.currentThread().getState(); is the code to display the status of a thread.

That declares a variable named state and initializes it with the state of the thread in which it is executed. (https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html

That code snippet doesn't display anything, but the subsequent System.out.println("status " + state); will write a representation of the state out to the console.

I have no way of knowing know whether Thread.State corresponds to what you call "status", but calling Thread.currentThread().getState(); yields no information because the state of any thread will always be RUNNABLE while it is calling getState().

[I need to know] how to assign different threads to different processors

I bet you don't.

What you are asking about is called "processor affinity." Most programs rely on the operating system to automatically schedule runnable threads on available CPUs. It's only very sophisticated programs (and note, "sophisticated" does not always equal "good") that need to tinker with it.

There is no standard way to set the CPU affinity of Java threads, but there might be some means that you can use on your particular operating system. You can google "Java processor affinity" for more info.

[I need to know] How to display the status of the processor

You're going to have to explain what "status" means and also, which processor, and when?

Depending on what "status" means, If you write code to ask for the "status" of the same processor that the code is running on, then the answer probably will never change. Just like how the result of Thread.currentThread().getState() never changes.

Processor "status" is the sort of thing that sysadmins of big data centers like to see plotted on charts, but they seldom are useful within a program unless, once again, you are doing something very sophisticated.

[I need to know] how to generate error messeges.

The simplest way to do that is to use System.out.println(...) or System.err.println(...)

Maybe you really meant to ask something else, like how to know when to report an error, or how to handle exceptions.

Most Java library routines throw an exception when something goes wrong. All you have to do in that case is completely ignore it. If you don't write any code to handle the exception, then your program will automatically print an error message and stop when the exception is thrown.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • 1
    "I bet you don't" gets an upvote from me. Great amount of patience and restraint here. – Gray Mar 24 '17 at 15:03
  • @james large i still didn't get anything but that processors Affinity was a millionaire answer was searching for ten days but was not able to get that term but still please can you tell me that did i made a parallel process or not (yes or no) – Moiz Farooqui Mar 24 '17 at 17:59
  • @MoizFarooqui, Your program starts four threads. Each of those four threads runs an instance of the same `Threads` class; and since the class constructor takes no arguments, each of the four threads will do exactly the same thing as the others. Yes, they will run concurrently, but I suspect that they may not be doing what you intended. Usually, different threads either do different things, or they do the same thing to different data. Your threads all do the same thing to the same data. – Solomon Slow Mar 24 '17 at 19:38
0

how to assign different threads to different processors (4 cores)

Let's leave this decision to java runtime. From programmer perspective, try to use the features available to you.

For effective utilizaiton of CPU cores, have a look at this question:

Java: How to scale threads according to cpu cores?

Regarding processor affinity, you can check posts:

Java thread affinity

http://tools.assembla.com/Behemoth/browser/Tests/JAVA/test/src/main/java/test/threads/ThreadAffinity.java ( written by BegemoT)

But you should avoid these type of things and focus on business logic.

how to generate error messeges(Memory Consistency Errors, etc.)

You can easily generate memory consistency errors.

Do not protect your code with thread safe constructs like synchronized or Lock and allow multiple threads to update data from your class.

Have a look at this documentation page

Refer to documentation links for better understanding of multi-threading concepts.

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211