我需要編寫兩個程序。 1順序(完成)和1平行,我做了一些事情,但我不知道我是否已經平行或不平行,我還需要知道:我是編程的初學者,需要一些幫助和建議
- If(Thread.State state = Thread 。.currentThread()的getState())是顯示一個螺紋
- 如何分配不同的線程不同的處理器的狀態(4芯)的代碼
- 如何顯示處理器
- 計算的狀態對於每個處理器
- 如何生成錯誤消息(內存一致性錯誤等)
以下是我的代碼:
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));
}
}
主要類:
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();
}
}
你需要谷歌上線程和Java教程,並從那裏開始。祝你好運。 – 7663233
做了很多,但仍然困惑,找不到任何東西,甚至遠程與我的需求相關。 –
嗨,歡迎來到Stackoverflow。請閱讀[tourguide](http://stackoverflow.com/tour),瞭解如何以正確的方式提問,以及如何使用計算器。 –