62,628
社区成员
发帖
与我相关
我的任务
分享
public class ThreadTest3 {
public static void main(String[] args) {
Count count1 = new Count(0, 100);
Count count2 = new Count(1000, 1100);
Thread thread1 = new Thread(count1);
Thread thread2 = new Thread(count2);
thread1.start();
thread2.start();
}
}
// 输出数字
class Count implements Runnable {
int start;
int end;
public Count(int start, int end) {
super();
this.start = start;
this.end = end;
}
@Override
public void run() {
for (int i = start; i <= end; i++) {
//Thread.currentThread().getName()打印线程名字
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
}
package com.demo;
public class Test06 {
public static void main(String[] args) {
new Thread(new SubThread2(0, 100)).start();
new Thread(new SubThread(1000, 1100)).start();
}
public static class SubThread implements Runnable {
private Integer startIndex;
private Integer endIndex;
public ThreadLocal<Integer> Count = new ThreadLocal<Integer>();
SubThread(Integer start, Integer end) {
startIndex = start;
endIndex = end;
}
@Override
public void run() {
Count.set(0);
for (int i = startIndex; i <= endIndex; i++) {
Count.set(Count.get() + i);
}
System.out.println(startIndex + "到" + endIndex + "共有" + Count.get());
}
}
public static class SubThread2 implements Runnable {
private Integer startIndex;
private Integer endIndex;
SubThread2(Integer start, Integer end) {
startIndex = start;
endIndex = end;
}
@Override
public void run() {
for (int i = startIndex; i <= endIndex; i++) {
System.out.println(i);
}
}
}
}