多线程的几个小程序,诠释多线程的基本概念
来源:互联网 发布:linux 访问url 命令 编辑:程序博客网 时间:2024/06/05 23:05
通过实现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 {@Overridepublic 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); }}
阅读全文
0 0
- 多线程的几个小程序,诠释多线程的基本概念
- 关于多线程的几个小程序。。。
- java多线程几个方法的基本概念
- java多线程几个方法的基本概念
- 黑马程序员---多线程的几个小问题
- 多线程 的基本概念
- 多线程的基本概念
- 多线程的基本概念
- 多线程的基本概念
- Linux 多线程的基本概念
- 多线程的基本概念
- 多线程的基本概念
- 多线程的基本概念
- 利用多线程加速程序的小程序
- 【java多线程学习】多线程的基本概念
- 多线程的几个细节
- 多线程实现的一个小程序
- java多线程模仿发牌的小程序
- 整理一批 国内外优秀设计团队 & 设计相关网站
- 【贪心】51Nod1615[跳跃的杰克]题解
- C# 7.1 Tuples解决多回传值及建立class的困扰
- C++中的static关键字的总结
- 类型转换小注意点
- 多线程的几个小程序,诠释多线程的基本概念
- 自动化运维工具Saltstack使用杂记
- 线程的状态
- REST四种请求(get,delete,put,post) 收集整理 之一
- 线程的同步
- 使用Hessian和Burlap发布远程接口服务步骤
- 线程模仿 ATM取钱
- 质因数分解
- Educational Codeforces Round 30(补题) A B略 C(贪心)