package thread;
public class ThreadDemo1 {
public static void main(String[] args) {
Thread t1 = new MyThread1();
Thread t2 = new MyThread2();
t1.start();
t2.start();
}
}
class MyThread1 extends Thread{
public void run() {
for(int i=0;i<1000;i++) {
System.out.println("你是谁啊?");
}
}
}
class MyThread2 extends Thread{
public void run() {
for(int i=0;i<1000;i++) {
System.out.println("我是查水表的!");
}
}
}
package thread;
public class ThreadDemo2 {
public static void main(String[] args) {
Runnable r1 = new MyRunnable1();
Runnable r2 = new MyRunnable2();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
}
}
class MyRunnable1 implements Runnable{
public void run() {
for(int i=0;i<1000;i++) {
System.out.println("你是谁啊?");
}
}
}
class MyRunnable2 implements Runnable{
public void run() {
for(int i=0;i<1000;i++) {
System.out.println("我是查水表的!");
}
}
}
package thread;
public class ThreadDemo3 {
public static void main(String[] args) {
Thread main = Thread.currentThread();
System.out.println("运行main方法的线程:"+main);
dosome();
Thread t = new Thread() {
public void run() {
Thread t = Thread.currentThread();
System.out.println("自定义线程:"+t);
dosome();
}
};
t.start();
}
public static void dosome() {
Thread t = Thread.currentThread();
System.out.println("运行dosome方法的线程是:"+t);
}
}
package thread;
public class ThreadDemo4 {
public static void main(String[] args) {
Thread main = Thread.currentThread();
String name = main.getName();
System.out.println("name:"+name);
long id = main.getId();
System.out.println("id:"+id);
int priority = main.getPriority();
System.out.println("优先级:"+priority);
boolean isAlive = main.isAlive();
System.out.println("isAlive:"+isAlive);
boolean isInterrupted = main.isInterrupted();
System.out.println("isInterrupted:"+isInterrupted);
boolean isDaemon = main.isDaemon();
System.out.println("isDaemon:"+isDaemon);
}
}
package thread;
public class PriorityDemo {
public static void main(String[] args) {
Thread max = new Thread() {
public void run() {
for(int i=0;i<10000;i++) {
System.out.println("max");
}
}
};
Thread min = new Thread() {
public void run() {
for(int i=0;i<10000;i++) {
System.out.println("min");
}
}
};
Thread norm = new Thread() {
public void run() {
for(int i=0;i<10000;i++) {
System.out.println("nor");
}
}
};
max.setPriority(Thread.MAX_PRIORITY);
min.setPriority(Thread.MIN_PRIORITY);
min.start();
norm.start();
max.start();
}
}
package thread;
public class SleepDemo {
public static void main(String[] args) {
System.out.println("程序开始了");
while(true) {
System.out.println("你好!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package thread;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字:");
String line = scanner.nextLine();
Integer num = Integer.parseInt(line);
for(;num>0;num--) {
System.out.println(num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("结束!");
}
}
package thread;
public class SleepDemo2 {
public static void main(String[] args) {
final Thread lin = new Thread() {
public void run() {
System.out.println("林:刚美完容,睡一会吧!");
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
System.out.println("林:干嘛呢!干嘛呢!干嘛呢!都破了相了!");
}
System.out.println("林:醒了!");
}
};
Thread huang = new Thread() {
public void run() {
System.out.println("黄:开始砸墙!");
for(int i=0;i<5;i++) {
System.out.println("黄:80!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println("咣当!");
System.out.println("黄:搞定!");
lin.interrupt();
}
};
lin.start();
huang.start();
}
}
package thread;
public class JoinDemo {
private static boolean isFinish = false;
public static void main(String[] args) {
Thread download = new Thread() {
public void run() {
System.out.println("down:开始下载图片...");
for(int i=1;i<=100;i++) {
System.out.println("down:"+i+"%");
try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
}
System.out.println("down:下载图片完毕!");
isFinish = true;
}
};
Thread show = new Thread() {
public void run() {
System.out.println("show:开始显示图片");
try {
download.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!isFinish) {
throw new RuntimeException("加载图片失败!");
}
System.out.println("show:显示图片完毕!");
}
};
download.start();
show.start();
}
}
package thread;
public class SyncDemo {
public static void main(String[] args) {
Table table = new Table();
Thread t1 = new Thread() {
public void run() {
while(true) {
int bean = table.getBean();
Thread.yield();
System.out.println(getName()+":"+bean);
}
}
};
Thread t2 = new Thread() {
public void run() {
while(true) {
int bean = table.getBean();
Thread.yield();
System.out.println(getName()+":"+bean);
}
}
};
t1.start();
t2.start();
}
}
class Table{
private int beans = 20;
public synchronized int getBean() {
if(beans==0) {
throw new RuntimeException("没有豆子了!");
}
Thread.yield();
return beans--;
}
}
package thread;
public class SyncDemo2 {
public static void main(String[] args) {
Shop shop = new Shop();
Thread t1 = new Thread() {
public void run() {
shop.buy();
}
};
Thread t2 = new Thread() {
public void run() {
shop.buy();
}
};
t1.start();
t2.start();
}
}
class Shop{
public void buy() {
try {
Thread t = Thread.currentThread();
System.out.println(t.getName()+":正在挑衣服...");
Thread.sleep(5000);
synchronized (this) {
System.out.println(t.getName()+":正在试衣服...");
Thread.sleep(5000);
}
System.out.println(t.getName()+":结账离开");
} catch (Exception e) {
}
}
}
package thread;
public class SyncDemo3 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
Foo.dosome();
}
};
Thread t2 = new Thread() {
public void run() {
Foo.dosome();
}
};
t1.start();
t2.start();
}
}
class Foo{
public synchronized static void dosome() {
Thread t = Thread.currentThread();
System.out.println(t.getName()+":正在运行dosome方法");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t.getName()+":运行dosome方法完毕");
}
}
package thread;
public class SyncDemo4 {
public static void main(String[] args) {
Boo boo = new Boo();
Thread t1 = new Thread() {
public void run() {
boo.methodA();
}
};
Thread t2 = new Thread() {
public void run() {
boo.methodB();
}
};
t1.start();
t2.start();
}
}
class Boo{
public synchronized void methodA() {
Thread t = Thread.currentThread();
try {
System.out.println(t.getName()+":正在运行A方法");
Thread.sleep(5000);
System.out.println(t.getName()+":运行A方法完毕");
} catch (Exception e) {
}
}
public void methodB() {
synchronized (this) {
Thread t = Thread.currentThread();
try {
System.out.println(t.getName()+":正在运行B方法");
Thread.sleep(5000);
System.out.println(t.getName()+":运行B方法完毕");
} catch (Exception e) {
}
}
}
}
package threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolDemo {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(2);
for(int i=0;i<5;i++) {
Runnable runn = new Runnable() {
public void run() {
Thread t = Thread.currentThread();
try {
System.out.println(t.getName()+":正在运行任务...");
Thread.sleep(5000);
System.out.println(t.getName()+":运行任务完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadPool.execute(runn);
System.out.println("指派了一个任务给线程池");
}
threadPool.shutdownNow();
System.out.println("线程池停止了!");
}
}