java复习笔记3

  Thread

  java的 线程Thread

运行下面代码

复制代码
class ThreadClass extends Thread{
    int i = 4;
    String name = "";
    public ThreadClass ( String name ) {
        this.name = name;
    }
    public void run() {
        while(i--!=0) {
            System.out.println("name is "+ this.name +"; i = " + i);
        }
    }
}
public class ThreadDemo {
    public static void main ( String args[])  {
        new ThreadClass("name").start();
        new ThreadClass("age").start();
    }
}
复制代码

 

  线程Runnable

运行下面代码

复制代码
class RunnableClass implements Runnable {
    int count = 10;
    public void run () {
        while(this.count!=0) {
            this.count--;
            System.out.println(this.count);
        };
    }
};

public class RunnableDemo{
    public static void main( String args[] ){
        RunnableClass run = new RunnableClass();
        new Thread(run).start();
    }
}
复制代码

 

  线程名字

  Thread的名字:Thread.currentThread().getName();

运行下面代码

复制代码
class ThreadDemo implements Runnable{
    public void run() {
        for(int i=0 ;i < 4; i++) {
            System.out.println(" index : "+i+", name is"+ Thread.currentThread().getName());
        };
        System.out.println("");
    }
}

public class  CurrentThread {
    public static void main(String args[]) {
        ThreadDemo th =  new ThreadDemo();
        new Thread(th,"heheda").start();
        new Thread(th,"dada").start();
    }
}
复制代码

   

  isAlive

  线程isAlive

运行下面代码

复制代码
class ThreadDemo implements Runnable{
    public void run() {
        for(int i=0 ;i < 4; i++) {
            System.out.println(" index : "+i+", name is"+ Thread.currentThread().getName());
        };
        System.out.println("");
    }
}

public class  CurrentThread {
    public static void main(String args[]) {
        ThreadDemo th =  new ThreadDemo();
        Thread t = new Thread(th,"heheda");
        t.start();
        System.out.println("th is acitve "+t.isAlive());
        new ThreadDemo().run();
        new ThreadDemo().run();
        System.out.println("th is acitve "+t.isAlive());
    }
}
复制代码

 

  join

  线程的强制运行join

运行下面代码

复制代码
class ThreadTest implements Runnable{
    public int i=0;
    public void run() {
        for(; i<66; i++) {
            System.out.println(Thread.currentThread().getName()+ " : " + i);
        }
    }

}
public class JoinDemo {
    public static void main(String args[]) throws Exception{
        Runnable run = new ThreadTest();
        Thread t = new Thread(run,"th0");
        t.start();
        for(int i=0; i<66; i++) {
            if(i>10) {
                t.join();
            }
            System.out.println(Thread.currentThread().getName()+ " : " + i);
        }

    }
}
复制代码

 

  线程sleep

运行下面代码

复制代码
class ThreadTest implements Runnable{
    public void run(){
        for(int i = 0; i<40 ; i++) {
            try{
                Thread.currentThread().sleep(500);
            }catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println("i = " + i);
        }
    }
}
public class ThreadSleepDemo {
    public static void main(String args[]) {
        Thread t = new Thread(new ThreadTest());
        t.start();
    }
}
复制代码

 

  线程DEMO

运行下面代码

复制代码
class ThreadTest extends Thread{
    public String name = "";
    public int times = 0;
    public ThreadTest( String name, int times ) {
        this.name = name;
        this.times = times;
    }
    public void run() {
        try{
            Thread.currentThread().sleep(this.times);
        }catch(Exception e) {
            e.printStackTrace();
        };
        System.out.println(this.name+"<<==>>"+this.times);
    }
}
public class ThreadDemo1 {
    public static void main(String args[]) {
        new ThreadTest("name",1000).start();
        new ThreadTest("name",2000).start();
        new ThreadTest("name",3000).start();
    }
}
复制代码

 

  synchronized代码块

运行下面代码

复制代码
class Ticket implements Runnable{
    int ticket  = 10;
    public void run() {
        synchronized(this) {

            while(true) {
                if(ticket<=0) {
                    break;
                };
                try{
                    Thread.currentThread().sleep(100);
                }catch(Exception e) {
                    e.printStackTrace();
                }
                System.out.println("now : " + ticket--);
            };

        };
    }
}
public class SellTicket {
    public static void main( String args[] ) {
        Ticket run = new Ticket();
        new Thread( run ).start();
        new Thread( run ).start();
        new Thread( run ).start();
        new Thread( run ).start();
    }
}
复制代码

  

  死锁

  synchronized代码块, 要注意死锁的情况, 你可以想象成是requireJS相互依赖的问题:

运行下面代码

复制代码
class Ticket implements Runnable{
    int ticket  = 10;
    public void run() {
        this.sell();
    }
    public  synchronized void  sell() {
        while(true) {
            if(ticket<=0) {
                break;
            };
            try{
                Thread.currentThread().sleep(100);
            }catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println("now : " + ticket--);
        };
    }
}
public class SellTicket {
    public static void main( String args[] ) {
        Ticket run = new Ticket();
        new Thread( run ).start();
        new Thread( run ).start();
        new Thread( run ).start();
        new Thread( run ).start();
    }
}
复制代码

 

  java泛型

  java的泛型必须指定构造函数

运行下面代码

复制代码
class Info<T> {
    public T str;
    public void setStr(T str) {
        this.str = str;
    }
    public T getStr() {
        return this.str;
    }
}
public class GenericsDemo {
    public static void main (String args[]) {
        Info<Integer> info = new Info<Integer>();
        info.setStr(1);
        System.out.println( info.getStr() );

        Info<String> info0 = new Info<String>();
        info0.setStr("heheda");
        System.out.println( info0.getStr() );
    }
}
复制代码

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/5228192.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(195)  评论(0编辑  收藏  举报
历史上的今天:
2014-03-03 最近新学的小东西和单词

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示