黑马程序员技术交流社区
标题:
开启线程
[打印本页]
作者:
王雷1
时间:
2013-11-27 14:46
标题:
开启线程
本帖最后由 王雷1 于 2013-11-29 13:41 编辑
哪位大神能帮忙给写一个在成员位置上的开启一个线程的匿名内部类,非常感谢
作者:
″先森丶玹°
时间:
2013-11-27 15:55
public class StartFromMethod {
private Thread t;
private int number;
private int count = 1;
public StartFromMethod(int number) {
this.number = number;
}
public void runTask() {
if (t == null) {
t = new Thread() {
public void run() {
while (true) {
System.out.println("Thread-" + number + " run " + count
+ " time(s)");
if (++count == 3)
return;
}
}
};
t.start();
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
new StartFromMethod(i).runTask();
}
}
复制代码
作者:
冯晓骏
时间:
2013-11-28 22:49
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
RunThread.runThread();
for(int i = 0 ; i < 10000 ; i ++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
class RunThread{
public static void runThread(){
Executors.newSingleThreadExecutor().execute(
new Runnable(){
public void run(){
for(int i = 0 ; i < 10000 ; i ++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
});
}
}
复制代码
作者:
殷挥笔
时间:
2013-11-28 22:49
public class Demo2_Thread {
/**
* @param args
*/
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T1" + "\t" + i);
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T2" + "\t" + i);
}
}
};
t.setDaemon(true);
t.start();
t2.start();
}
}
复制代码
上课的时候写的,希望能帮到你
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2