package com.alpha;
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt, "自己的线程A").start();
new Thread(mt).start();
new Thread(mt, "自己的线程B").start();
}
}
package com.alpha;
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt, "自己的线程对象").start();
mt.run();
}
}
main
自己的线程对象
package com.alpha;
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10000; i ++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt, "自己的线程对象A").start();
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
Thread thread = new Thread(() -> {
System.out.println("线程休眠");
try {
Thread.sleep(10000);
System.out.println("休眠结束");
} catch (InterruptedException e) {
System.out.println("产生中断");
}
});
thread.start();
Thread.sleep(100);
if (!thread.isInterrupted()) {
System.out.println("中断线程");
thread.interrupt();
}
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
Thread mainThread = Thread.currentThread();
Thread thread = new Thread(() -> {
for (int i = 0; i < 50; i ++) {
if (i == 20) {
try {
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " ==> " + i);
}
});
thread.start();
for (int i = 0; i < 50; i ++) {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " ==> " + i);
}
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
Thread mainThread = Thread.currentThread();
Thread thread = new Thread(() -> {
for (int i = 0; i < 50; i ++) {
if (i % 3 == 0) {
System.out.println(Thread.currentThread().getName() + " 让步");
Thread.yield();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " ==> " + i);
}
});
thread.start();
for (int i = 0; i < 50; i ++) {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " ==> " + i);
}
}
}
package com.alpha;
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i ++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt, "自己的线程对象A");
Thread t2 = new Thread(mt, "自己的线程对象B");
Thread t3 = new Thread(mt, "自己的线程对象C");
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
public class MainClass { // 主类
public static void main(String[] args) throws Exception {
System.out.println(Thread.currentThread().getPriority());
}
}