歡迎您光臨本站 註冊首頁

Java向Runnable線程傳遞參數方法實例解析

←手機掃碼閱讀     limiyoyo @ 2020-06-03 , reply:0

java Runnable接口:是一個接口,它裡面只有一個run()方法,沒有start()方法,繼2113承Runnable並實現這個方法就可以實現多線程了,但是5261這個run()方法不能自4102己調用,必須由系統來調用。

向線程中傳遞數據的三種方法:

一、通過構造函數傳遞參數

 public class MyThread1 extends Thread { private String name; public MyThread1(String name) { this.name = name; } public void run() { System.out.println("hello " + name); } public static void main(String[] args) { Thread thread = new MyThread1("world"); thread.start(); } }


二、通過變量和方法傳遞數據

 public class MyThread2 implements Runnable { private String name; public void setName(String name) { this.name = name; } public void run() { System.out.println("hello " + name); } public static void main(String[] args) { MyThread2 myThread = new MyThread2(); myThread.setName("world"); Thread thread = new Thread(myThread); thread.start(); } }


三、通過回調函數傳遞數據

 class Data { public int value = 0; } class Work { public void process(Data data, Integer numbers) { for (int n : numbers) { data.value += n; } } } public class MyThread3 extends Thread { private Work work; public MyThread3(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 thread = new MyThread3(new Work()); thread.start(); } }

[limiyoyo ] Java向Runnable線程傳遞參數方法實例解析已經有235次圍觀

http://coctec.com/docs/java/show-post-236816.html