在android中创建线程并返回数组
时间: 2023-05-17 16:06:12 浏览: 54
在Android中创建线程并返回数组的代码示例:
```java
public class MyThread extends Thread {
private int[] array;
public MyThread(int[] array) {
this.array = array;
}
@Override
public void run() {
// 在这里进行耗时操作,例如排序
Arrays.sort(array);
}
public int[] getSortedArray() {
return array;
}
}
// 在主线程中调用
int[] array = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
MyThread thread = new MyThread(array);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
int[] sortedArray = thread.getSortedArray();
```
这段代码创建了一个继承自Thread的MyThread类,该类接收一个int数组作为参数,并在run方法中对该数组进行排序。在主线程中创建MyThread对象并启动线程,然后使用join方法等待线程执行完毕,最后调用getSortedArray方法获取排序后的数组。