public class ThreadTest {
public static void main(String[] args) {
SubTest subTest = new SubTest();
subTest.start();
}
}
class SubTest extends Thread{
@Override
public void run() {
for (int i = 2; i < 100; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName() +" "+ Thread.currentThread().getState() +" i = " + i + "***");
}
}
}
}
public class Test {
public static void main(String[] args) {
sub sub1 = new sub();
new Thread(sub1).start();
/*
Thread的构造器
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
Thread的run方法
public void run() {
if (target != null) {
target.run();
}
}
*/
}
}
class sub implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("i = " + i);
}
}
}
public class Test {
public static void main(String[] args) {
HelloThread helloThread = new HelloThread("线程二");
// helloThread.setName("线程一");
helloThread.setPriority(10);
helloThread.start();
// 主线程
Thread.currentThread().setName("主线程");
Thread.currentThread().setPriority(1);
System.out.println(Thread.currentThread().getName());
for (int i = 0; i < 100; i++) {
if (i % 2 ==1){
System.out.println(Thread.currentThread().getPriority());
System.out.println(Thread.currentThread().getName()+" : " + i);
}
}
}
}
class HelloThread extends Thread{
public HelloThread(){}
public HelloThread(String threadname){
super(threadname);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
// try {
// sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
if (i % 2 ==0){
System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()+" : " + i);
}
if (i % 20 == 0){
yield();
}
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容