public class ThreadDemo10 {
public static void main(String[] args){
Thread t=new Thread("线程1")
{
@Override
public void run() {
try{
Thread.sleep(60*60*1000);
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
t.start();}
}
打开jconsole
查看线程情况
public class ThreadDemo11 {
public static void main(String[] args)
{
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(60*60*1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
},"Runnable-Thread");
thread.start();
}
}
代码修改 重新打开jconsole 查看线程情况
`
import java.util.Random;
public class ThreadDemo12 {
public static void main(String[] args)
{
ThreadGroup group=new ThreadGroup("thread-group");
Runnable runTask=new Runnable() {
@Override
public void run() {
int num=(1+new Random().nextInt(3));
try{
Thread.sleep(num*1000);
}catch (InterruptedException e)
{e.printStackTrace();}
System.out.println("选手到达终点"+num+"s");
}
};
Thread t1=new Thread(group,runTask);
Thread t2=new Thread(group,runTask);
Thread t3=new Thread(group,runTask);
t1.start();
t2.start();
t3.start();
while (group.activeGroupCount()!=0){
}
System.out.println("宣布成绩") ;
}
}
import java.util.Random;
public class ThreadDemo12 {
public static void main(String[] args)
{//创建一个线程分组(创建一个比赛)
ThreadGroup group=new ThreadGroup("thread-group");
//定义一个公共的任务(线程任务)
Runnable runTask=new Runnable() {
@Override
public void run() {//任务
int num=(1+new Random().nextInt(3));
try{//到达终点时间
Thread.sleep(num*1000);
}catch (InterruptedException e)
{e.printStackTrace();}
System.out.println("选手到达终点"+num+"s");
}
};
//3 线程 运动员
Thread t1=new Thread(group,runTask);
Thread t2=new Thread(group,runTask);
Thread t3=new Thread(group,runTask);
//开炮
t1.start();
t2.start();
t3.start();
//到达后宣布成绩
while (group.activeGroupCount()!=0){
}
System.out.println("宣布成绩") ;
}
}
单线程名称
public class ThreadDemo13 {
public static void main(String[] args)
{
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程ID:"+t.getId());
System.out.println("线程名称:"+t.getName());
}
},"线程1");
thread.start();
}
}
多线程名称 状态
public static void main(String[] args)
{
Runnable runnable=new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程ID:"+t.getId());
System.out.println("线程名称:"+t.getName());
System.out.println();
}
};
Thread thread=new Thread(runnable,"线程1");
thread.start();
System.out.println();
Thread thread2=new Thread(runnable,"线程2");
thread2.start();
}
}
加入时间
public class ThreadDemo13 {
public static void main(String[] args) throws InterruptedException {
Runnable runnable=new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程ID:"+t.getId());
System.out.println("线程名称:"+t.getName());
}
};
Thread thread=new Thread(runnable,"线程1");
thread.start();
Thread.sleep(500);
System.out.println();
Thread thread2=new Thread(runnable,"线程2");
thread2.start();
}
}
线程状态
public class ThreadDemoBystate
{
public static void main(String[] args)
{
Thread t=new Thread(new Runnable() {
@Override
public void run() {
Thread thread=Thread.currentThread();
System.out.println("线程状态2:"+thread.getState());
}
});
System.out.println("线程的状态:"+t.getState());
t.start();
}
}
public class ThreadDemoBystate
{
public static void main(String[] args) throws InterruptedException {
Thread t=new Thread(new Runnable() {
@Override
public void run() {
Thread thread=Thread.currentThread();
System.out.println("线程状态2:"+thread.getState());
}
});
System.out.println("线程的状态:"+t.getState());
t.start();
Thread.sleep(500);
System.out.println("线程状态3:"+t.getState());
}
}
线程优先级
/*线程优先级*/
public class ThreadDempByPriority
{
public static void main(String[] args)
{
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程优先级:"+t.getPriority());
}
});
System.out.println("线程优先级2"+thread.getPriority());
thread.start();
Thread.sleep(500);
System.out.println("线程优先级3:"+ thread.getPriority());
}
}
/*线程优先级*/
public class ThreadDempByPriority
{
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程优先级:"+t.getPriority());
}
});
System.out.println("线程优先级2"+thread.getPriority());
thread.start();
Thread.sleep(500);
System.out.println("线程优先级3:"+ thread.getPriority());
}
}
public class ThreadDemo13 {
public static void main(String[] args) throws InterruptedException {
Runnable runnable=new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println("线程ID:"+t.getId());
System.out.println("线程名称:"+t.getName());
System.out.println("线程优先级"+t.getPriority());
}
};
Thread thread=new Thread(runnable,"线程1");
thread.start();
Thread.sleep(500);
System.out.println();
Thread thread2=new Thread(runnable,"线程2");
thread2.start();
Thread thread3=new Thread(runnable,"线程3");
thread3.start();
}
}
每个线程保证代码从上往下执行
优先级讨论 执行顺序 多线程
import java.awt.*;
public class ThreadByPriority2 {
private final static int MAXCOUNT=100;
public static void main(String[] args) {
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
//得到当前现线程
Thread t=Thread.currentThread();
int priority=t.getPriority();
for (int i = 0; i < 1000; i++) {
System.out.println(t.getName()+"-优先级:"+priority);
}
}
});//设置优先就 10
t1.setPriority(Thread.MAX_PRIORITY);
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
int priority=t.getPriority();
for (int i = 0; i < MAXCOUNT; i++) {
System.out.println(t.getName()+"-优先级"+priority);
}
}
},"线程2");
t2.setPriority(Thread.MIN_PRIORITY);
Thread t3=new Thread(new Runnable() {
@Override
public void run() {
Thread t=Thread.currentThread();
int priority=t.getPriority();
for (int i = 0; i <MAXCOUNT ; i++) {
System.out.println(t.getName()+"-优先级:"+priority);
}
}
},"线程3");
t3.setPriority(Thread.NORM_PRIORITY);
t2.start();
t1.start();
t3.start();
}
}
高优先级被执行概率更大‘
高概率高优先级先执行完