JAVA——Thread、Runnable线程实例
分别利用Thread和Runnable来实现两个数相加。
1.利用Thread
public class FactorialThread extends Thread{ int c; private int a,b; public FactorialThread(int a,int b) { this.a=a; this.b=b; } public void run() { c=a+b; System.out.println(c); } }
2.利用Runnable
class MyThread implements Runnable{ int c; private int a,b; MyThread(int a,int b){ this.a=a; this.b=b; } @Override public void run() { c=a+b; System.out.println(c); } }
3.主函数调用start()
ublic class main { public static void main(String[] args) { MyThread thread = new MyThread(4,6); FactorialThread mt1 = new FactorialThread(3,5); mt1.start(); Thread mt2=new Thread(thread);//Runnable不能直接调用start(); mt2.start(); } }
相关的概念参考:https://blog.csdn.net/kwame211/article/details/78963044

浙公网安备 33010602011771号