public class Main {
public static class TwoThreadAlive extends Thread {
public void run() {
for (int i = 0; i < 10; i++)printMsg();
}
public void printMsg() {
Thread t = Thread.currentThread();//获取当前前程
String name = t.getName();//获取当前线程的名字
System.out.println("name=" + name);//打印当前线程的名字
}
}
public static void printMainMsg() {
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println("name=" + name);
}
public static void main(String[] args) {
TwoThreadAlive tt = new TwoThreadAlive();
tt.setName("Thread");
System.out.println("before start(), tt.isAlive()=" + tt.isAlive());
tt.start();//start之后就会执行他的run
System.out.println("just after start(), tt.isAlive()=" + tt.isAlive());
// 注意:下面的循环不应该调用tt.printMsg(),因为它会在主线程中运行
// 而不是在tt线程中运行。如果您想要在主线程中打印消息,应该创建一个新的方法。
for (int i = 0; i < 10; i++)printMainMsg(); // 调用一个新的方法来在主线程中打印消息
try {
tt.join(); // 等待tt线程结束
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("The end of main(), tt.isAlive()=" + tt.isAlive());
}
}
class TwoThreadGetName extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
printMsg();
}
}
public void printMsg() {
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println("name=" + name);
}
}
public class Main{
public static void main(String[] args) {
TwoThreadGetName tt = new TwoThreadGetName();
tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
}
}
class MyThread extends Thread{
boolean waiting = true;
boolean ready = false;
MyThread(){}//初始化
public void run() {
String Th_name = Thread.currentThread().getName();
System.out.println(Th_name + "starting");
while(waiting)System.out.println("waiting:"+waiting);
System.out.println("waiting...");
startWait();
try {
Thread.sleep(1000);
}
catch(Exception exc) {
System.out.println(Th_name + " interrupted.");
}
System.out.println(Th_name + " terminating.");
}
synchronized void startWait() {
try {
while(!ready) wait();//进入等待 等待ready=true;
}
catch(InterruptedException exc) {
System.out.println("wait() interrupted");
}
}
synchronized void notice() {
ready = true;
notify();
}
}
public class Main{
static void showThreadStatus(Thread t) {
System.out.println(t.getName() + "Alive:=" + t.isAlive() + " State:=" + t.getState());
}
public static void main(String args[]) throws Exception {
MyThread tt = new MyThread();
tt.setName("MyThread-1");
// tt.start();
showThreadStatus(tt); // 打印出状态
// 设置 waiting 为 false,以便线程可以继续执行
tt.waiting = false;
// 启动线程
tt.start();
// 给线程一些时间来启动并进入等待状态
Thread.sleep(50);
// 通知线程继续执行
tt.notice();
// 等待线程完成执行
while (tt.isAlive()) {
System.out.println("alive");
}
// 打印最终状态
showThreadStatus(tt);
}
}
//优先级设置
class SimplePriorities extends Thread{
private int countDown = 5;
private volatile double d = 0;
//Constructor
public SimplePriorities(int priority) {
setPriority(priority);//配置优先级
start();//构造之后直接调用
}
public String toString() {
return super.toString() + ": " + countDown;
}//super.toString() 调用返回当前线程的字符串表示
public void run() {
while(true) {
for(int i=1;i<100000;i++) {
d = d+ (Math.PI + Math.E)/(double)i;
System.out.println(this);//输出线程的信息 默认是toString()
if(--countDown == 0) return;
}
}
}
}
public class Main{
public static void main(String[] args) {
new SimplePriorities(Thread.MAX_PRIORITY);//Thread-0 先执行
for(int i = 0; i < 5; i++)
new SimplePriorities(Thread.MIN_PRIORITY);
}
}
class ThreadID extends ThreadLocal {
private int nextID;
public ThreadID() {
nextID = 10001;
}
private synchronized Integer getNewID() {
Integer id = new Integer(nextID);
nextID++;
return id;
}
protected Object initialValue() {//在调用get之后会进行initialValue()
print("in initialValue()");
return getNewID();
}
public int getThreadID() {//获取线程id 用get方法得到一个Integer 然后返回id.intValue();
Integer id = (Integer) get();
return id.intValue();
}
private static void print(String msg) {
String name = Thread.currentThread().getName();
System.out.println(name + ": " + msg);
}
}
public class Main extends Object implements Runnable {
private ThreadID var;
public Main(ThreadID v) {
this.var = v;
}
public void run() {
try {
print("var getThreadID =" + var.getThreadID());
Thread.sleep(2000);
print("var getThreadID =" + var.getThreadID());
} catch (InterruptedException x) {
}
}
private static void print(String msg) {
String name = Thread.currentThread().getName();
System.out.println(name + ": " + msg);
}
public static void main(String[] args) {
ThreadID tid = new ThreadID();
Main shared = new Main(tid);
try {//初始化一个Thread 继承的实例 名称
Thread threadA = new Thread(shared, "threadA");
threadA.start();
Thread.sleep(500);
Thread threadB = new Thread(shared, "threadB");
threadB.start();
Thread.sleep(500);
Thread threadC = new Thread(shared, "threadC");
threadC.start();
} catch (InterruptedException x) {
}
}
}
class SleepingThread extends Thread {
private int countDown = 5;//控制线程运行的次数
private static int threadCount = 0;//记录创建线程的数量
public SleepingThread() {//super()在规定名字
super(" " + ++threadCount);//这是在调用Thread类的构造方法,
//将转换后的字符串作为线程的名称传递给父类。
start();
}
public String toString() {//默认格式super.toString()是线程的信息
return "#" + getName() + ": " + countDown;
}
public void run() {
while (true) {
System.out.println(this);//this用来打印toString()
if (--countDown == 0)
return;
try {
sleep(100);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
SleepingThread[] threads = new SleepingThread[5];
for (int i = 0; i < 5; i++) {
threads[i] = new SleepingThread();
}
for (SleepingThread t : threads) {
t.join();// // 当前线程将等待t线程结束
}//如果没有join的话主线程可能会提前退出
}
}
class ThreadInterrupt extends Thread
{
public void run()
{
try
{
sleep(50000); // 延迟50秒
}
catch (InterruptedException e)
{
System.out.println(e.getMessage()+"aaa"); //已经被抓到了
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new ThreadInterrupt();
thread.start();
System.out.println("在50秒之内按任意键中断线程!");
//程序在这里暂停,等待用户输入。当用户按下任意键并回车时,程序将继续执行。
System.in.read();
//调用 interrupt() 方法来请求中断线程。如果线程当前正在 sleep()
//它将抛出 InterruptedException 并退出 sleep() 方法.
thread.interrupt();
// try {
// Thread.sleep(1000);//这样不会触发
// thread.interrupt();
// }catch(InterruptedException e) {
// System.out.println("aaa");
// }
thread.join();
System.out.println("线程已经退出!");
}
}
// Java 程序 - 演示线程状态
class thread implements Runnable
{
public void run()
{
// thread2 - 超时等待
try
{
Thread.sleep(1500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("State of thread1 while it called join() method on thread2 -"+
Main.thread1.getState());
try
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public class Main implements Runnable
{
public static Thread thread1;
public static Main obj;
public static void main(String[] args)
{
obj = new Main();
thread1 = new Thread(obj);
// 创建 thread1,现在是初始状态
System.out.println("State of thread1 after creating it - " + thread1.getState());
thread1.start();
// thread1 - 就绪状态
System.out.println("State of thread1 after calling .start() method on it - " +
thread1.getState());
}
public void run()
{
thread myThread = new thread();
Thread thread2 = new Thread(myThread);
// 创建 thread1,现在是初始状态
System.out.println("State of thread2 after creating it - "+ thread2.getState());
thread2.start();
// thread2 - 就绪状态
System.out.println("State of thread2 after calling .start() method on it - " +
thread2.getState());
// moving thread1 to timed waiting state
try
{
//moving - 超时等待
Thread.sleep(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("State of thread2 after calling .sleep() method on it - "+
thread2.getState() );
try
{
// 等待 thread2 终止
thread2.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("State of thread2 when it has finished it's execution - " +
thread2.getState());
}
}