panxw / gist Goto Github PK
View Code? Open in Web Editor NEWcode segments
code segments
package test;
public class TryCatchTest {
public static void main(String[] args) {
test(5); //由于IllegalArgumentException归属uncheck异常,调用方法时,编译器是不报错的
//对unckeck异常不建议进行catch,如果进对其catch,的确可以直接往后执行,但是否希望的流程?
try{
test(-1);
}catch(IllegalArgumentException e){
//e.printStackTrace();
System.out.println("get IllegalStateException");
}
//unckeck异常 但最好的办法是在调用之前,对传入的参数进行检查
int a = -5;//举例,假设a是外部传入参数
if(a>0){
test(a);
}else {
System.out.println("paramter not allowed");
return;
}
System.out.println("main end");
}
public static void test(int a) throws IllegalArgumentException {
if(a<0){
throw new IllegalArgumentException ();
}
}
}
package test;
public class ThreadLocalTest {
public static class MyRunnable implements Runnable {
private ThreadLocal threadLocal = new ThreadLocal(); //threadLocal都是绑定到当前线程上的一个变量,Runnable代码被多个线程访问时,都各自独立
private ThreadLocal threadLocalb = new ThreadLocal();
private int runableVaule=0; //由于Runnable代码块被多个线程使用,runableVaule变量也就可以被多个线程修改
@Override
public void run() {
// 一旦创建了一个ThreadLocal变量,你可以通过如下代码设置某个需要保存的值
int value = (int) (Math.random() * 100D);
threadLocal.set(value); //实际是调用thread.threadLocals.set(thread, 100)
threadLocalb.set(Thread.currentThread().getId());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
// 可以通过下面方法读取保存在ThreadLocal变量中的值
synchronized (MyRunnable.class) {
runableVaule++;
System.out.println("name:"+Thread.currentThread().getId()); //取到的是公有变量
System.out.println("-------threadLocal value-------"
+ threadLocal.get()); //取到的是当前线程上面的变量的值, 实际是调用thread.threadLocals.get(thread)
System.out.println("-------threadLocalb value-------"
+ threadLocalb.get()); //取到的是当前线程上面的变量的值, 实际是调用thread.threadLocals.get(thread)
System.out.println("-------runableVaule-------"
+ runableVaule); //取到的是公有变量
}
}//其实, run就好比Servlet的doGet或doPost方法,每个请求都执行到同一个代码块,但是它们处理过程确是相互独立的,不存在线程安全问题
}
public static void main(String[] args) {
MyRunnable sharedRunnableInstance = new MyRunnable();
Thread thread1 = new Thread(sharedRunnableInstance);
Thread thread2 = new Thread(sharedRunnableInstance);
Thread thread3 = new Thread(sharedRunnableInstance);
Thread thread4 = new Thread(sharedRunnableInstance);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
int next(){
for(;;){
return 1;
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.