package day06;
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new MyThread1();
Thread t1 =new MyThread2();
t.start();
t1.start();
}
}
class MyThread1 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("你好,我是张翠花!");
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("你好,我是赵狗蛋!嘻嘻...");
}
}
}
package day06;
public class ThreadDemo02 {
public static void main(String[] args) {
Runnable r = new MyThread3();
Runnable r2 = new MyThread4();
Thread t = new Thread(r);
Thread t2 = new Thread(r2);
t.start();
t2.start();
}
}
class MyThread3 implements Runnable{
public void run() {
for(int i=0;i<100;i++){
System.out.println("我是翠花,来自西北地区。");
}
}
}
class MyThread4 implements Runnable{
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println("你好,我是狗蛋,来自新疆");
}
}
}
package day06;
public class ThreadDemo03 {
public static void main(String[] args) {
Thread t = new Thread(){
public void run(){
for(int i=0;i<100;i++){
System.out.println("李狗蛋,你好,我是张翠花。");
}
}
};
Runnable r = new Runnable(){
public void run(){
for(int i=0;i<100;i++){
System.out.println("翠花,我...我,你好,我是狗蛋..");
}
}
};
Thread th = new Thread(r);
t.start();
th.start();
}
}
package day06;
public class CurrentThreadDemo05 {
public static void dosome(){
Thread t = Thread.currentThread();
System.out.println("运行dosome方法的线程是:"+t);
}
public static void main(String[] args) {
Thread main = Thread.currentThread();
System.out.println(main);
Thread t2 = new Thread(){
public void run(){
Thread t = Thread.currentThread();
System.out.println("自定义线程:"+t);
dosome();
}
};
t2.start();
}
}
package day06;
public class InfoDemo_06 {
public static void main(String[] args) {
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println(name);
int p =t.getPriority();
System.out.println(p);
boolean b= t.isAlive();
System.out.println(b);
boolean b1 = t.isDaemon();
System.out.println(b1);
boolean b2 = t.isInterrupted();
System.out.println(b2);
}
}
package day06;
public class ProiorityDemo_07 {
public static void main(String[] args) {
Thread t = new Thread(){
public void run(){
for(int i=0;i<1000;i++){
System.out.println("max");
}
}
};
Thread t2 = new Thread(){
public void run(){
for(int i=0;i<1000;i++){
System.out.println("nor");
}
}
};
Thread t3 = new Thread(){
public void run(){
for(int i=0;i<1000;i++){
System.out.println("min");
}
}
};
t.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t.start();
t2.start();
t3.start();
}
}
package day06;
public class SleepBlockDemo_08 {
public static void main(String[] args) {
Thread lin = new Thread(){
public void run(){
System.out.println("林:刚美容完,睡一会儿吧...");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
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) {
e.printStackTrace();
}
}
System.out.println("黄:咣当,搞定啦!");
lin.interrupt();
}
};
lin.start();
huang.start();
}
}
package day06;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SleepBlockDemo_09 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(550,500);
JPanel panel = new JPanel();
panel.setSize(500,500);
frame.setContentPane(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
Thread t = new Thread(){
public void run(){
int i=0;
while(true){
i = i==0?1:0;
if(i==0){
panel.setBackground(Color.BLACK);
}
if(i==1){
panel.setBackground(Color.PINK);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
}
package day06;
public class DaemoThreadDemo_10 {
public static void main(String[] args) {
Thread rose = new Thread(){
public void run(){
for(int i=0;i<5;i++){
System.out.println("rose:let me go!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("rose:啊啊啊....");
System.out.println("噗通..");
}
};
Thread jack = new Thread(){
public void run(){
while(true){
System.out.println("jack:you jump,I jump");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
jack.setDaemon(true);
rose.start();
jack.start();
System.out.println("我是'主线程main'");
}
}
package day06;
public class JoinDemo_11 {
public static void main(String[] args) {
Thread download = new Thread(){
public void run(){
try {
System.out.println("电影开始下载...");
for(int i=0;i<100;i++){
System.out.println("下载进度"+i+"%");
Thread.sleep(50);
}
System.out.println("图片下载完毕!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread show = new Thread(){
public void run(){
try {
download.join();
System.out.println("开始播放电影!:");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
download.start();
show.start();
}
}
package day06;
import javax.management.RuntimeErrorException;
public class SyncDemo_12 {
public static void main(String[] args) {
Table table = new Table();
Thread t1 = new Thread(){
public void run(){
while(true){
int b = table.getBean();
Thread.yield();
System.out.println(getName()+":"+b);
}
}
};
Thread t2 = new Thread(){
public void run(){
while(true){
int b1 = table.getBean();
Thread.yield();
System.out.println(getName()+":"+b1);
}
}
};
t1.start();
t2.start();
}
}
class Table{
private int bean = 20;
public synchronized int getBean(){
if(bean == 0){
throw new RuntimeException("没有豆子了!");
}
Thread.yield();
return bean--;
}
}