Thread thread = new Thread("t1")
{
@Override
public void run()
{
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
};
thread.start();
如果调用start,则输出是t1
Thread thread = new Thread("t1")
{
@Override
public void run()
{
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
};
thread.run();
Thread thread = new Thread("t1")
{
@Override
public void run()
{
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
};
public void run(){//线程t1
while(true){
Thread.yield();
}
}
t1.interrupt();
public void run(){
while(true)
{
if(Thread.currentThread().isInterrupted())
{
System.out.println("Interruted!");
break;
}
Thread.yield();
}
}
public void run(){
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("Interruted!");
break;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Interruted When Sleep");
//设置中断状态,抛出异常后会清除中断标记位
Thread.currentThread().interrupt();
}
Thread.yield();
}
}
package test;
public class Test
{
static Object u = new Object();
static TestSuspendThread t1 = new TestSuspendThread("t1");
static TestSuspendThread t2 = new TestSuspendThread("t2");
public static class TestSuspendThread extends Thread
{
public TestSuspendThread(String name)
{
setName(name);
}
@Override
public void run()
{
synchronized (u)
{
System.out.println("in " + getName());
Thread.currentThread().suspend();
}
}
}
public static void main(String[] args) throws InterruptedException
{
t1.start();
Thread.sleep(100);
t2.start();
t1.resume();
t2.resume();
t1.join();
t2.join();
}
}
package test;
public class Test
{
public volatile static int i = 0;
public static class AddThread extends Thread
{
@Override
public void run()
{
for (i = 0; i < 10000000; i++)
;
}
}
public static void main(String[] args) throws InterruptedException
{
AddThread at = new AddThread();
at.start();
at.join();
System.out.println(i);
}
}
package test;
public class Test
{
public static class DaemonThread extends Thread
{
@Override
public void run()
{
for (int i = 0; i < 10000000; i++)
{
System.out.println("hi");
}
}
}
public static void main(String[] args) throws InterruptedException
{
DaemonThread dt = new DaemonThread();
dt.start();
}
}
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
package test;
public class Test
{
public static class High extends Thread
{
static int count = 0;
@Override
public void run()
{
while (true)
{
synchronized (Test.class)
{
count++;
if (count > 10000000)
{
System.out.println("High");
break;
}
}
}
}
}
public static class Low extends Thread
{
static int count = 0;
@Override
public void run()
{
while (true)
{
synchronized (Test.class)
{
count++;
if (count > 10000000)
{
System.out.println("Low");
break;
}
}
}
}
}
public static void main(String[] args) throws InterruptedException
{
High high = new High();
Low low = new Low();
high.setPriority(Thread.MAX_PRIORITY);
low.setPriority(Thread.MIN_PRIORITY);
low.start();
high.start();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有