从在UI线程中运行代码的角度来看,之间有什么区别:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
要么
MainActivity.this.myView.post(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
和
private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
protected void onPostExecute(Bitmap result) {
Log.d("UI thread", "I am the UI thread");
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个单身人士课程:
public class Singleton {
private static Singleton istance = null;
private Singleton() {}
public synchronized static Singleton getSingleton() {
if (istance == null)
istance = new Singleton();
return istance;
}
public void work(){
for(int i=0; i<10000; i++){
Log.d("-----------", ""+i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
多个线程正在调用work()函数:
public class Main {
public static void main(String[] args) {
new Thread (new Runnable(){
public void run(){
Singleton s = Singleton.getSingleton();
s.work();}
}).start();
System.out.println("main thread");
new Thread(new Runnable() {
public void run() {
Singleton s = …Run Code Online (Sandbox Code Playgroud) 我注意到以下C代码给出了"警告:初始化从指针目标类型中丢弃限定符",但它仍然按预期编译和运行(输出'W'字符).
#include <stdio.h>
int main(int argc, char *argv[])
{
char buffer[20] = {'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};
const char* p = &buffer[0];
char* c = (p + 6);
printf("%c\n",*c);
}
Run Code Online (Sandbox Code Playgroud)
在C++中,相当类似的代码根本不会编译抱怨"错误:从'const char*'到'char*'的无效转换"
#include <iostream>
using namespace std;
int main()
{
char buffer[20] = {'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};
const char* p = &buffer[0];
char* c = p + 6;
cout << *c;
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是什么原因?
是否有可能修复C++代码以使其编译(和行为)就像它的C对应物一样?
更好的解释:感谢您的所有答案,但大多数人没有得到我真正的问题所以我会尝试更详细地解释.
我正在使用用C编写的库.标题中的函数原型是这样的:
void parse (const char* p, uint16_t len, uint8_t is_eof);
Run Code Online (Sandbox Code Playgroud)
在这个函数的实现中,碰巧运行代码就好了
char* c = p …Run Code Online (Sandbox Code Playgroud) android ×1
android-ui ×1
android-view ×1
arduino ×1
c ×1
c++ ×1
concurrency ×1
java ×1
pointers ×1
ragel ×1
singleton ×1