使用runnable将参数传递给线程

java
阅读 45 收藏 0 点赞 0 评论 0

threadWithParams.java
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.concurrent.*;
public class Solution {
	
	public static void main(String[] args){
        System.out.println("Thread will print the parameter \"APPLE\"");
		myRunnable r=new myRunnable("APPLE");
        Thread thread_object=new Thread(r);
        thread_object.start();
	}
    
}

class myRunnable implements Runnable
{
	//The information to be saved
	//and later passed to the thread is
	//stored as a parameter here
    String to_print;
    
    myRunnable(String to_print){
    	//here we save it when myRunnable is built
        this.to_print = to_print;
    }
    public void run(){
    	//Here we indicate how
    	//we use the parameter passed.
        System.out.println(to_print);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号