android-线程 (从java开始)案例

来源:互联网 发布:淘宝开店教程ppt下载 编辑:程序博客网 时间:2024/05/18 15:27

最近发现对于线程理解很模糊了,以前学过的东西不用的话总是忘得很快,很纠结,有什么好办法的小伙伴给支个招哈,

首先先了解一下java线程,很多人都在博客上写上什么是线程,然后怎么样,不过我感觉还是简单粗暴一点直接上代码,有些东西就直接打注释,简单粗暴。

可能代码顺序有点----,主要是看看注释,回忆一下

package com.threads;/** * java 启动线程两种方法  * 1,实现Runnable接口 Thread thread = new Thread(target); * target为Runnable接口类型 *  Runnable方法中只有一个方法 public void run();定义线程的运行体 * 2,定义一个Thread方法实现run方法 *  class MyThread extends Thread{ *   public void run(){  *   } *    } * 生成该类的对象 MyThread myThread = new MyThread(); *  * 推荐使用第一种 对于java 继承是只能单继承,实现比较单一 *  * 线程常用的方法  * 1,sleep  * 2,interrupt  * 3,join  * 4,yield   该方法与sleep()类似,只是不能由用户指定暂停多长时间,并且yield()方法只能让同优先级的线程有执行的机会。 *  *  * 线程优先级 runnerTh.setPriority() *  * 停止线程 不要使用stop方法 可以使用boolean进行控制 *  * @author Administrator * */public class javaThread {public static void main(String[] args) throws InterruptedException {Runner runner = new Runner();Thread thread = new Thread(runner);thread.start();// 把 thread合并到主线程 相当于方法调用,等这个thread执行完成之后在执行主线程的内容。thread.join();RunnerTh runnerTh = new RunnerTh();runnerTh.start();// 把优先级提高runnerTh.setPriority(Thread.NORM_PRIORITY + 5);/** * 停止线程  调用方法停止 */RunnerStop runnerStop = new RunnerStop();Thread thread2 = new Thread(runnerStop);thread2.start();//调用方法使线程结束runnerStop.StopThread();for (int i = 0; i < 10; i++) {System.out.println("MainThread + " + i);}}}/** * 实现Runnable接口 *  * @author Administrator * */class Runner implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubtry {// sleep方法 睡眠 1000毫秒new Thread().sleep(1000);for (int i = 0; i < 10; i++) {System.out.println("Runner + " + i);if (1 % 3 == 0) {// 让出内存 给其他的线程执行new Thread().yield();}}} catch (InterruptedException e) {e.printStackTrace();}}}class RunnerStop implements Runnable {private boolean flag = false;@Overridepublic void run() {// TODO Auto-generated method stubwhile (!flag) {for (int i = 0; i < 10; i++) {System.out.println("Runner + " + i);if (1 % 3 == 0) {// 让出内存 给其他的线程执行new Thread().yield();}}}}public void StopThread() {flag = true;}}/** *  * @author Administrator * */class RunnerTh extends Thread {@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < 10; i++) {System.out.println("RunnerTh + " + i);}}}

线程同步


/** * 线程同步 * 就是线程在访问同一个方法的时候一个一个的来,不要在一个线程访问的时候其他线程也在访问 * 线程同步使用synchronized * @author Administrator * */public class Text implements Runnable {  Timer timer = new Timer();public static void main(String[] args) {Text text = new Text();Thread thread = new Thread(text);Thread thread2 = new Thread(text);thread.setName("this is 1111111111");thread2.setName("this is 2222222222");thread.start();thread2.start();}@Overridepublic void run() {// TODO Auto-generated method stubtimer.add(Thread.currentThread().getName());}}class Timer {private static int num = 0;public //synchronizedvoid add(String name) {//synchronized (this) {//锁定当前对象。在执行的时候不会被其他的线程打断num++;try {new Thread().sleep(1);} catch (InterruptedException e) {}System.out.println(name + "-----------" + num);//}}}

android 也可以通过实现Runnable接口实现更新UI操作


public class MainActivity extends Activity {private TextView textView;private Button button;public Handler handler  ; protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView) findViewById(R.id.TextVie);button = (Button) findViewById(R.id.Main_Btn);handler = new Handler() {public void handleMessage(Message msg) {if (msg.what == 1) {textView.setText(msg.obj+"");}};};button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = new Message();message.what = 1;message.obj = "ssssss";handler.sendMessage(message);}}).start();; }});} }


0 0