多线程的几个小程序,诠释多线程的基本概念
2017-10-17 17:48
351 查看
通过实现Runnable来实现多线程:
通过继承Thread来实现多线程:
注意:继承了Thread,那么不用再new Thread来起线程,直接用Runner1.start()即可
通过Thread.join()方法来等待当前线程走完
下面的程序,如果join了,那么n就等于1000
在多线程中如何传递参数:
简单的是通过构造函数和set()来传值
复杂的是通过回调函数传值
下面的代码描述了回调传值的用法:
这个程序可能会输出"NULL":
因为线程可能还没有赋值就已经syso了。
解决办法是在start()后面加个join();
public class TestThread1{ public static void main(String[] args){ Runner1 r = new Runner1(); Thread t = new Thread(r); t.start(); //注意:t.run(); 是指方法调用,先执行Runner1()后,再往下执行。 for(int i=200; i<300; i++){ System.out.println("main--" + i); } } } class Runner1 implements Runnable{ public void run(){ for(int i=0; i<100; i++){ System.out.println("Runner1--" + i); } } }
public class Test1 implements Runnable { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread: " + i ); try { Thread.sleep((int)Math.random() * 200); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Test1 t1 = new Test1(); Thread t_1 = new Thread(t1); t_1.setName("aaa"); t_1.start(); for (int i = 0; i < 10; i++) { System.out.println("main: " + i ); try { Thread.sleep((int)Math.random() * 200); } catch (InterruptedException e) { e.printStackTrace(); } } } }
通过继承Thread来实现多线程:
public class TestThread1{ public static void main(String[] args){ Runner1 r = new Runner1(); r.start(); for(int i=200; i<300; i++){ System.out.println("main--" + i); } } } class Runner1 extends Thread{ public void run(){ for(int i=0; i<100; i++){ System.out.println("Runner1--" + i); } } }
注意:继承了Thread,那么不用再new Thread来起线程,直接用Runner1.start()即可
通过Thread.join()方法来等待当前线程走完
下面的程序,如果join了,那么n就等于1000
public static volatile int n = 0; public void run() { for (int i = 0; i < 10; i++, n++) try { sleep(3); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[100]; for (int i = 0; i < threads.length; i++) // 建立100个线程 threads[i] = new Test4(); for (int i = 0; i < threads.length; i++) // 运行刚才建立的100个线程 threads[i].start(); if (args.length > 0){ for (int i = 0; i < threads.length; i++){ // 100个线程都执行完后继续 threads[i].join(); } } System.out.println("n=" + Test4.n); }
在多线程中如何传递参数:
简单的是通过构造函数和set()来传值
复杂的是通过回调函数传值
下面的代码描述了回调传值的用法:
package com.mhm.test; public class Test5 extends Thread { private Work work; public Test5(Work work) { this.work = work; } public void run() { java.util.Random random = new java.util.Random(); Data data = new Data(); int n1 = random.nextInt(1000); int n2 = random.nextInt(2000); int n3 = random.nextInt(3000); work.process(data, n1, n2, n3); // 使用回调函数 System.out.println(String.valueOf(n1) + "+" + String.valueOf(n2) + "+" + String.valueOf(n3) + "=" + data.value); } public static void main(String[] args) { Thread t1 = new Test5(new Work()); Thread t2 = new Test5(new Work()); Thread t3 = new Test5(new Work()); t1.start(); t2.start(); t3.start(); } } class Data { public int value = 0; } class Work { public void process(Data data, Integer... numbers) { for (int n : numbers) { data.value += n; } } }
这个程序可能会输出"NULL":
因为线程可能还没有赋值就已经syso了。
解决办法是在start()后面加个join();
package com.mhm.test; public class Test6 extends Thread { private String value1; private String value2; public void run() { value1 = "通过成员变量返回数据"; value2 = "通过成员方法返回数据"; } public static void main(String[] args) throws Exception { Test6 thread = new Test6(); thread.start(); System.out.println("value1:" + thread.value1); System.out.println("value2:" + thread.value2); } }
相关文章推荐
- java多线程几个方法的基本概念
- [知了堂学习笔记]_Java多线程(1)-- 基本概念并开始创建多线程程序
- java多线程几个方法的基本概念
- JAVA多线程系列之基本概念
- 简明Python教程读书笔记-2 几个基本概念
- 多线程基本概念
- java多线程的基本概念和简单操作
- Objective-C基础笔记(1)基本概念和第一个程序
- 计算机程序的思维逻辑 (35) - 泛型 (上) - 基本概念和原理
- 面向对象的几个基本概念
- Java多线程的几个重要概念
- 多线程中的基本概念
- 几个基本的小程序(3)(素数/闰年)
- 关于多线程学习总结(一) 基本概念了解
- 《转》 压力测试中需要掌握的几个基本概念
- Java(一)程序的基本概念
- cocos2d-x从零入门04之几个基本概念
- 几个slr的基本概念
- 多线程基本概念介绍(Introduction and Concepts)
- iphone绘图的几个基本概念CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)