I need to write a small Java multithread program to utilise multi core. I want to run on both Windows and Android. But it seems Android uses something different from Java multithreading. Do I have to do two programs?
我需要编写一个小的Java多线程程序来使用多核。我想在Windows和Android上运行。但似乎Android使用了与Java多线程不同的东西。我必须做两个程序吗?
1 个解决方案
#1
1
No, You don't have to write two different set of code for Windows and Android, provided you are talking about multi-threading portion of your code.
不,如果您正在谈论代码的多线程部分,则不必为Windows和Android编写两组不同的代码。
Of course you will need two different executable for Android and Windows as Android normally require your code to be compiled and packaged as APK file.
当然,Android和Windows需要两个不同的可执行文件,因为Android通常需要将您的代码编译并打包为APK文件。
Regarding multi-threading, Android supports all Java standard library classes related to multi-threading. Android also provides few extra classes like AsyncTask etc. two abstract away complexities of multi-threading for an average Java programmer. But these android specific utility classes are implemeted on top of existing standard Java threading.
关于多线程,Android支持与多线程相关的所有Java标准库类。 Android还提供了一些额外的类,如AsyncTask等,这是一个普通Java程序员的两个抽象复杂的多线程。但是这些特定于android的实用程序类是在现有的标准Java线程之上实现的。
So a multi-threaded code written using standard java packages will work without modification on Android too.
因此,使用标准java包编写的多线程代码也可以在Android上进行修改而无需修改。
The code below will work perfectly on both Android and Windows.
下面的代码可以在Android和Windows上完美运行。
Runnable myRunnable = new Runnable(){
public void run(){
int a = 1000;
a = a * 200;
}
}
Thread myThread = new Thread(myRunnable);
myThread.start();
myThread.join();
ForkJoinTasks too is supported in Android. See details here https://developer.android.com/reference/java/util/concurrent/ForkJoinTask
Android中也支持ForkJoinTasks。详情请见https://developer.android.com/reference/java/util/concurrent/ForkJoinTask
#1
1
No, You don't have to write two different set of code for Windows and Android, provided you are talking about multi-threading portion of your code.
不,如果您正在谈论代码的多线程部分,则不必为Windows和Android编写两组不同的代码。
Of course you will need two different executable for Android and Windows as Android normally require your code to be compiled and packaged as APK file.
当然,Android和Windows需要两个不同的可执行文件,因为Android通常需要将您的代码编译并打包为APK文件。
Regarding multi-threading, Android supports all Java standard library classes related to multi-threading. Android also provides few extra classes like AsyncTask etc. two abstract away complexities of multi-threading for an average Java programmer. But these android specific utility classes are implemeted on top of existing standard Java threading.
关于多线程,Android支持与多线程相关的所有Java标准库类。 Android还提供了一些额外的类,如AsyncTask等,这是一个普通Java程序员的两个抽象复杂的多线程。但是这些特定于android的实用程序类是在现有的标准Java线程之上实现的。
So a multi-threaded code written using standard java packages will work without modification on Android too.
因此,使用标准java包编写的多线程代码也可以在Android上进行修改而无需修改。
The code below will work perfectly on both Android and Windows.
下面的代码可以在Android和Windows上完美运行。
Runnable myRunnable = new Runnable(){
public void run(){
int a = 1000;
a = a * 200;
}
}
Thread myThread = new Thread(myRunnable);
myThread.start();
myThread.join();
ForkJoinTasks too is supported in Android. See details here https://developer.android.com/reference/java/util/concurrent/ForkJoinTask
Android中也支持ForkJoinTasks。详情请见https://developer.android.com/reference/java/util/concurrent/ForkJoinTask