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("儿子买烟回来了!!");
}
}