我对JNI非常熟悉,我很想看到java.lang包中某些本机方法的机器特定实现.Thread#currentThread(), 例如.
我在[JDK_HOME]/jre/bin中找到了一堆DLL,但就像我说的那样,我试图找到源代码.
有谁知道可以找到原生源代码的位置?它是否可用,或者它是否被Sun分类(oops我的意思是"我们在其中赢得它"Oracle)?
我们不能直接在线程的对象上调用Runnable的run()方法,但是根据下面的程序,我们没有任何编译或运行时错误.为什么会这样?
public class ThreadCheck implements Runnable {
@Override
public void run() {
for (int i=0; i<10; ) {
System.out.println(++i);
}
}
public static void main(String[] args) {
Thread mythread = new Thread(new ThreadCheck());
mythread.run();
mythread.run();
mythread.start();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
我弄清楚了.
出于某种原因,这个线程的代码实际上是在UI线程上运行的.如果我突破它,UI就会停止.或者睡觉吧,UI停了.因此,在"ui"线程中不允许网络活动.
我没有使用过异步任务,因为我不知道循环它的正确方法.(调用它的新实例onPostExecute似乎是不好的做法,好像异步是一个关闭的任务.
我扩展了Thread.
public class SyncManager extends Thread {
public SyncManager(Context context){
sdb = new SyncManagerDBHelper(context);
mContext = context;
}
@Override
public void run() {
while(State == RUNNING) {
try{
SyncRecords(); // Break point here = UI freeze.
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(10000); // So also causes UI freeze.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void startThread() {
Log.i("SyncManager", "start called");
if((State == PAUSED || State == STOPPED) …Run Code Online (Sandbox Code Playgroud) 我有一个简单的线程测试:
package Thread;
class Sum extends Thread {
int low, up, S;
public Sum(int a, int b) {
low = a;
up = b;
S = 0;
System.out.println("This is Thread " + this.getId());
}
@Override
public void run() {
for (int i = low; i < up; i++) {
S += i;
}
System.out.println(this.getId() + ":" + S);
}
}
public class Tester {
public static void main(String agrs[]) {
Sum T1 = new Sum(1, 100);
T1.start();
Sum T2 = …Run Code Online (Sandbox Code Playgroud)