1 thread对象
java代码
thread t = new thread(
new thread(){
@override
public void run() {
while(true){
try {
thread.sleep(1000);
} catch (interruptedexception e) {
e.printstacktrace();
}
system.out.println("1: "+thread.currentthread().getname());
system.out.println("2: "+this.getname());
}
}
}
);
t.start();
2 runnable接口
java代码
thread t2 = new thread(
new runnable(){
@override
public void run() {
while(true){
try {
thread.sleep(1000);
} catch (interruptedexception e) {
e.printstacktrace();
}
system.out.println("3: "+thread.currentthread().getname());
}
}
}
);
t2.start();
下面会执行哪部分代码呢!
java代码
//只要重写了run方法,你从构造函数传递进去的线程对象就不会在执行
new thread(new runnable(){
@override
public void run() {
while(true){
try {
thread.sleep(1000);
} catch (interruptedexception e) {
e.printstacktrace();
}
system.out.println("runnable: "+thread.currentthread().getname());
}
}
}
){
@override
public void run() {
while(true){
try {
thread.sleep(1000);
} catch (interruptedexception e) {
e.printstacktrace();
}
system.out.println("thread: "+thread.currentthread().getname());
}
}
}.start();
输出结果如下:
log代码
3: thread-2
1: thread-1
2: thread-0
thread: thread-3
以下代码来自于:java编程思想第四版,说明加了是否加线程的区别!
counter1 .java没有加线程的表现!
java代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class counter1 extends applet {
private static final long serialversionuid = 1l;
private int count = 0;
private button onoff = new button("toggle"), start = new button("start");
private textfield t = new textfield(10);
private boolean runflag = true;
public void init() {
add(t);
start.addactionlistener(new startl());
add(start);
onoff.addactionlistener(new onoffl());
add(onoff);
}
@suppresswarnings("static-access")
public void go() {
while (true) {
try {
thread.currentthread().sleep(100);
} catch (interruptedexception e) {
}
if (runflag)
t.settext(integer.tostring(count++));
}
}
class startl implements actionlistener {
public void actionperformed(actionevent e) {
go();
}
}
class onoffl implements actionlistener {
public void actionperformed(actionevent e) {
runflag = !runflag;
}
}
public static void main(string[] args) {
counter1 applet = new counter1();
frame aframe = new frame("counter1");
aframe.addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
system.exit(0);
}
});
aframe.add(applet, borderlayout.center);
aframe.setsize(300, 200);
applet.init();
applet.start();
aframe.setvisible(true);
}
}
counter2i.java加了线程结果大不一样了,加了个内部类,它继承了thread
java代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class counter2i extends applet {
private static final long serialversionuid = 1l;
private class separatesubtask extends thread {
int count = 0;
boolean runflag = true;
separatesubtask() {
start();
}
public void run() {
while (true) {
try {
sleep(100);
} catch (interruptedexception e) {
}
if (runflag)
t.settext(integer.tostring(count++));
}
}
}
private separatesubtask sp = null;
private textfield t = new textfield(10);
private button onoff = new button("toggle"), start = new button("start");
public void init() {
add(t);
start.addactionlistener(new startl());
add(start);
onoff.addactionlistener(new onoffl());
add(onoff);
}
class startl implements actionlistener {
public void actionperformed(actionevent e) {
if (sp == null)
sp = new separatesubtask();
}
}
class onoffl implements actionlistener {
public void actionperformed(actionevent e) {
if (sp != null)
sp.runflag = !sp.runflag; // invertflag();
}
}
public static void main(string[] args) {
counter2i applet = new counter2i();
frame aframe = new frame("counter2i");
aframe.addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
system.exit(0);
}
});
aframe.add(applet, borderlayout.center);
aframe.setsize(300, 200);
applet.init();
applet.start();
aframe.setvisible(true);
}
}
counter3.java它实现了runnable接口
java代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class counter3 extends applet implements runnable {
private static final long serialversionuid = 1l;
private int count = 0;
private boolean runflag = true;
private thread selfthread = null;
private button onoff = new button("toggle"), start = new button("start");
private textfield t = new textfield(10);
public void init() {
add(t);
start.addactionlistener(new startl());
add(start);
onoff.addactionlistener(new onoffl());
add(onoff);
}
@suppresswarnings("static-access")
public void run() {
while (true) {
try {
selfthread.sleep(100);
} catch (interruptedexception e) {
}
if (runflag)
t.settext(integer.tostring(count++));
}
}
class startl implements actionlistener {
public void actionperformed(actionevent e) {
if (selfthread == null) {
selfthread = new thread(counter3.this);
selfthread.start();
}
}
}
class onoffl implements actionlistener {
public void actionperformed(actionevent e) {
runflag = !runflag;
}
}
public static void main(string[] args) {
counter3 applet = new counter3();
frame aframe = new frame("counter3");
aframe.addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
system.exit(0);
}
});
aframe.add(applet, borderlayout.center);
aframe.setsize(300, 200);
applet.init();
applet.start();
aframe.setvisible(true);
}
}
更多java之线程(匿名内部类)。
