点击button暂停
public class MainActivity extends AppCompatActivity { @BindView(R.id.button) Button button; @BindView(R.id.first_textview) TextView textView; Handler mHandler; volatile boolean flag=true; Object object=new Object(); MThread mThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); textView.setText(String.valueOf(1)); mHandler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); textView.setText(String.valueOf(msg.what)); } }; mThread=new MThread(); mThread.start(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!mThread.isAlive()){ mThread=new MThread(); mThread.start(); return; } if(flag==true){ flag=!flag; } else{ synchronized (object){ flag=!flag; object.notify(); } } } }); } public class MThread extends Thread{ @Override public void run() { System.out.println("enter thread"); for(int i=20;i>=0;i--){ if(flag==false){ synchronized (object) { try { object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } System.out.println(i); mHandler.sendEmptyMessage(i); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }}
两个handler:
public class MainActivity extends AppCompatActivity { @BindView(R.id.button) Button button; @BindView(R.id.textview) TextView textView; Handler mFirstHandler; Handler mSecondHandler; volatile boolean flag = true; Object object = new Object(); SecondThread mThread; FirstThread mFirstThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); ButterKnife.bind(this); textView.setText(String.valueOf(1)); mSecondHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { textView.setText(String.valueOf(msg.what)); return true; } }); mThread = new SecondThread(); mThread.start(); mFirstThread = new FirstThread(); mFirstThread.start(); } public class SecondThread extends Thread { @Override public void run() { Looper.prepare(); Looper.loop(); System.out.println("*****************"); mFirstHandler = new Handler() { @Override public void handleMessage(Message msg) { mSecondHandler.sendEmptyMessage(msg.what); } }; } } public class FirstThread extends Thread { @Override public void run() { System.out.println("enter first thread"); for (int i = 20; i >= 0; i--) { Message message = Message.obtain(); message.what = i; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } mFirstHandler.sendMessage(message); } } }}
使用ThreadHandler
public class MainActivity extends AppCompatActivity { @BindView(R.id.button) Button button; @BindView(R.id.textview) TextView textView; Handler mFirstHandler; Handler mSecondHandler; Thread t=new FirstThread(); volatile boolean flag = true; Object object = new Object(); HandlerThread handlerThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); ButterKnife.bind(this); textView.setText(String.valueOf(1)); mSecondHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { textView.setText(String.valueOf(msg.what)); return true; } }); handlerThread=new HandlerThread("1"); handlerThread.start(); mFirstHandler=new Handler(handlerThread.getLooper()){ @Override public void handleMessage(Message msg) { mSecondHandler.sendEmptyMessage(msg.what); } }; t.start(); } public class FirstThread extends Thread { @Override public void run() { System.out.println("enter first thread"); for (int i = 20; i >= 0; i--) { Message message = Message.obtain(); message.what = i; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } mFirstHandler.sendMessage(message); } } }}