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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2014-03-03 最近新学的小东西和单词