public class TicketThread extends Thread { private int ticketCount = 100; @Override public void run() { for (; ticketCount>0; ticketCount--) { String desc = String.format("%d %d 当前余票为%d张", System.currentTimeMillis(), getId(), ticketCount); System.out.println(desc); } } }
public class ThreadTest { public static void main(String[] arg) { //方式一,同一Thread实例多次start,重复调用会抛出异常。因为start是同步方法,不允许同一时刻多次运行 // TicketThread seller = new TicketThread(); // seller.start(); // seller.start(); // seller.start(); //方式二,创建多个Thread实例分别start,三个线程每个各卖100张,总共卖了300张票 // new TicketThread().start(); // new TicketThread().start(); // new TicketThread().start(); //方式三,只创建一个Runnable实例,使用该实例启动三个线程一起卖票,总共卖了100张票 Runnable mSeller = new Runnable() { private int ticketCount = 100; @Override public void run() { for (; ticketCount > 0; ticketCount--) { String desc = String.format("%d 当前余票为%d张", System.currentTimeMillis(), ticketCount); System.out.println(desc); } } }; new Thread(mSeller).start(); new Thread(mSeller).start(); new Thread(mSeller).start(); } }
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
public final boolean postDelayed(Runnable r, long delayMillis) { return sendMessageDelayed(getPostMessage(r), delayMillis); } private static Message getPostMessage(Runnable r) { Message m = Message.obtain(); m.callback = r; return m; }
Handler mHandler = new Handler(); Message msg = mHandler.obtainMessage();
Handler mHandler = new Handler(); Message msg = Message.obtain(mHandler);
import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; @SuppressLint("DefaultLocale") public class ThreadActivity extends Activity implements OnClickListener { private TextView tv_thread; private long mBeginTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_thread); Button btn_single = (Button) findViewById(R.id.btn_single); Button btn_multi = (Button) findViewById(R.id.btn_multi); btn_single.setOnClickListener(this); btn_multi.setOnClickListener(this); tv_thread = (TextView) findViewById(R.id.tv_thread); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_single) { mBeginTime = System.currentTimeMillis(); new TicketThread().start(); } else if (v.getId() == R.id.btn_multi) { mBeginTime = System.currentTimeMillis(); new Thread(mSeller).start(); new Thread(mSeller).start(); new Thread(mSeller).start(); } } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); long cost = System.currentTimeMillis() - mBeginTime; String desc = String.format("消息标识%d:%s耗时%d毫秒", msg.what, (String)(msg.obj), cost); tv_thread.setText(desc); } }; private Runnable mSeller = new Runnable() { private int ticketCount = 10000; @Override public void run() { for (; ticketCount > 0; ticketCount--) { String desc = String.format("%d 当前余票为%d张", System.currentTimeMillis(), ticketCount); System.out.println(desc); } if (ticketCount <= 0) { Message msg = Message.obtain(mHandler); msg.obj = "多个窗口卖票"; mHandler.sendMessage(msg); } } }; private class TicketThread extends Thread { private int ticketCount = 10000; @Override public void run() { for (; ticketCount>0; ticketCount--) { String desc = String.format("%d %d 当前余票为%d张", System.currentTimeMillis(), getId(), ticketCount); System.out.println(desc); } if (ticketCount <= 0) { Message msg = mHandler.obtainMessage(); msg.obj = "单个窗口卖票"; mHandler.sendMessage(msg); } } } }