Java 在android中实现三个不同线程的三个计数器并在UI中显示
我正在尝试在android中实现多线程。我的意图是构建三个不同的线程,从每个线程我将在UI中更新三个不同的textView 这三个线程都正常运行Java 在android中实现三个不同线程的三个计数器并在UI中显示,java,android,multithreading,Java,Android,Multithreading,我正在尝试在android中实现多线程。我的意图是构建三个不同的线程,从每个线程我将在UI中更新三个不同的textView 这三个线程都正常运行 public class Thread1 implements Runnable{ @Override public void run() { while(true){ System.out.println("Thread1 Thread details :"+
public class Thread1 implements Runnable{
@Override
public void run() {
while(true){
System.out.println("Thread1 Thread details :"+Thread.currentThread());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
不同线程日志消息的内部正确显示。但是当我想显示任何toast消息
或txtView消息
时,它不起作用
09-09 17:43:17.882 1383-1406/com.example.root.multithreading0004 I/System.out﹕ Thread2 Thread details :Thread[pool-1-thread-2,5,main]
09-09 17:43:17.883 1383-1407/com.example.root.multithreading0004 I/System.out﹕ Thread3 Thread details :Thread[pool-1-thread-3,5,main]
09-09 17:43:18.826 1383-1405/com.example.root.multithreading0004 I/System.out﹕ Thread1 Thread details :Thread[pool-1-thread-1,5,main]
09-09 17:43:18.917 1383-1406/com.example.root.multithreading0004 I/System.out﹕ Thread2 Thread details :Thread[pool-1-thread-2,5,main]
09-09 17:43:18.917 1383-1407/com.example.root.multithreading0004 I/System.out﹕ Thread3 Thread details :Thread[pool-1-thread-3,5,main]
09-09 17:43:19.826 1383-1405/com.example.root.multithreading0004 I/System.out﹕ Thread1 Thread details :Thread[pool-1-thread-1,5,main]
09-09 17:43:19.924 1383-1406/com.example.root.multithreading0004 I/System.out﹕ Thread2 Thread details :Thread[pool-1-thread-2,5,main]
09-09 17:43:19.924 1383-1407/com.example.root.multithreading0004 I/System.out﹕ Thread3 Thread details :Thread[pool-1-thread-3,5,main]
09-09 17:43:20.827 1383-1405/com.example.root.multithreading0004 I/System.out﹕ Thread1 Thread details :Thread[pool-1-thread-1,5,main]
这是我的完整代码:
package com.example.root.multithreading0004;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends ActionBarActivity {
public static int num1 = 0;
public static int num2 = 0;
public static int num3 = 0;
public static int num4 = 0;
TextView tv2;
TextView tv3;
TextView tv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv4 = (TextView)findViewById(R.id.textView4);
ExecutorService executorService = Executors.newFixedThreadPool(3);
System.out.println("Main START");
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
Thread3 thread3 = new Thread3();
executorService.submit(thread1);
executorService.submit(thread2);
executorService.submit(thread3);
System.out.println("Main END");
}
public class Thread1 implements Runnable{
@Override
public void run() {
while(true){
System.out.println("Thread1 Thread details :"+Thread.currentThread());
//Toast.makeText(MainActivity.this,"I am Thread1", Toast.LENGTH_SHORT).show();
//tv2.append("I am Thread1:" + num1++ +"\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread2 implements Runnable{
@Override
public void run() {
while(true){
System.out.println("Thread2 Thread details :"+Thread.currentThread());
//Toast.makeText(MainActivity.this,"I am Thread2",Toast.LENGTH_SHORT).show();
//tv3.append("I am Thread2:" + num2++ +"\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread3 implements Runnable{
@Override
public void run() {
while(true){
System.out.println("Thread3 Thread details :"+Thread.currentThread());
//Toast.makeText(MainActivity.this,"I am Thread3",Toast.LENGTH_SHORT).show();
//tv4.append("I am Thread3:" + num4++ +"\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
请告诉我丢失了什么?您正在尝试修改主(UI)线程之外的视图。那是不可能的。您应该像这样更新视图:
runOnUiThread(new Runnable() {
@Override
public void run() {
//update textviews
}
});
注意:runOnUiThread()
是活动的方法
更新
@Override
public void run() {
while(true){
System.out.println("Thread1 Thread details :"+Thread.currentThread());
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//update textviews
tv2.setText("I am Thread1:" + num1++ +"\n");
}
});
//
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
“它不工作了。”它做了什么?may helpHi Yazazzello,谢谢你宝贵的指导。我是android的新手。你能告诉我在哪里写这个代码吗?请在我的密码中注明。我将非常感谢你。