线程 上

package thread;


public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
demo.method2("这个是线程程序!!");
}
public void method1(String str){
System.out.println(str);
}
public void method2(String str){
method1(str);
}

}


package thread;


public class ThreadDemo01 {
public static void main(String[] args) {
myThread mt = new myThread();
System.out.println("MainThread:执行完毕!!!");

myRunnable runnable = new myRunnable();
Thread td = new Thread(runnable);

mt.start();
td.start();


for (int i = 0; i < 10; i++) {
System.out.println("MainThread:"+i);
}
}
}


class myThread extends Thread{
@Override
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("myThread:"+i);
}
}
}
class myRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("myRunnable:"+i);
}
}
}

package thread;




public class ThreadDemo02 {
public static void main(String[] args) {
myThread02 thread02 = new myThread02();
//设置优先级
thread02.setPriority(Thread.MAX_PRIORITY);
//设置线程的名字
thread02.setName("线程One:  ");
myRunnable02 runnable02 = new myRunnable02();
Thread t = new Thread(runnable02,"线程Two:");
//设置优先级
//t.setPriority(Thread.MIN_PRIORITY);
//设置 优先级 为 1-10  类型
t.setPriority(5);
thread02.start();
t.start();
}
}
class  myThread02 extends Thread{
public void run(){
for (int i = 0; i < 100; i+=2) {
System.out.println(this.getName()+"偶数"+i);
}
}
}
class myRunnable02 implements Runnable{


@Override
public void run() {
for (int i = 1; i < 100; i+=2) {
System.out.println(Thread.currentThread().getName()+"奇数"+i);
}

}

}

package thread;


public class ThreadException {
public static void main(String[] args) {
Producer producer = new Producer();
Cosumer cosumer = new Cosumer();

producer.setPriority(1);
cosumer.setPriority(10);

//通过调用thread.setDaemon(true)把用户线程变成一个守护线程
//守护线程在jvm退出时自动结束,JVM不会等守护线程
//用户线程没有的话 会直接退出
producer.setDaemon(true);
cosumer.setDaemon(true);

producer.start();
cosumer.start();
}
}
class Producer extends Thread{
public void run(){
for (int i = 0; i < 50; i++) {
System.out.println("I AN Producer"+i);
//线程让步 :让不不是绝对的,在让步也有可能不是全都在最后
Thread.yield();
}
}
}
class Cosumer extends Thread{
public void run(){
for (int i = 0; i < 50; i++) {
System.out.println("I AM Consumer"+i);
}
}
}



package thread;


public class ThreadJoinDemo {
public static void main(String[] args) {
System.out.println("------儿子与爸爸的美好的一天!!------");
Father father = new Father();
Thread thread = new Thread(father);
thread.start();
}
}
class Father implements Runnable{
@Override
public void run() {
System.out.println("爸爸让儿子去买烟 。。。。。。");
Thread thread = new Thread(new Son());
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("儿子买烟回来了,爸爸很高兴@!!!!");
}

}
class Son implements Runnable{
public void run() {
System.out.println("儿子去买烟。。。");
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
System.out.println("儿子去买烟的第"+i+"分钟");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("儿子买烟回来了!!");
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值