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

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

首先先了解一下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 {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			// 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;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (!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 {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (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();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		timer.add(Thread.currentThread().getName());
	}
}

class Timer {
	private static int num = 0;

	public 
//	synchronized
	void 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() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		 
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
					Message message = new Message();
					message.what = 1;
					message.obj = "ssssss";
					handler.sendMessage(message);
					}
				}).start();;
				
				 
			}
		});
	}
	 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值