您的位置:首页 > 移动开发 > 微信开发

多线程的几个小程序,诠释多线程的基本概念

2017-10-17 17:48 351 查看
通过实现Runnable来实现多线程:

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: