A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小爷邱烙   /  2014-11-27 19:34  /  3323 人查看  /  79 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. for(int i=0;i<8;i++){//一个整数是4个字节,32位,四位一移,移动八次
  2.         int temp = num & 15;
  3.         if(temp>9)
  4.                 System.out.println((char)(temp-10+'A'));
  5.         else
  6.                 System.out.println(temp);
  7.         num = num >>>4;
  8. }
复制代码


回复 使用道具 举报
  1. char[] chs = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  2. char[] arr = new char[8];//字符数组的默认值为'\u0000',即Unicode的0000,为空格
  3. int pos = arr.length;//定义指针,用于循环
  4. while(num!=0){
  5.         int temp = num & 15;
  6.         arr[--pos] = chs[temp];//倒序,方便输出
  7.         num = num >>>4;
  8. }
  9. //此时,正负的问题也解决了
复制代码


回复 使用道具 举报
  1. public static void trans(int num,int base,int offset){
  2.         if(num==0){
  3.                 System.out.println(0);
  4.                 return ;
  5.         }
  6.         char[] chs = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  7.         char[] arr = new char[32];
  8.         int pos = arr.length;
  9.         while(num!=0){
  10.                 int temp = num & base;
  11.                 arr[--pos] = chs[temp];
  12.                 num = num >>> offset;
  13.         }
  14.         for(int i=pos;i<arr.length;i++){
  15.                 System.out.print(arr[i]);
  16.         }
  17. }
复制代码


回复 使用道具 举报
  1. class Person{
  2.         private int age;
  3.         Person(int age){
  4.                 this.age = age;
  5.         }
  6.         public boolean compare(Person p){
  7.                 return this.age == p.age;//描述功能时,还没有对象。此时需要用到调用该函数的对象,用this
  8.         }
  9. }
  10. class PersonDemo{
  11.         public static void main(String[] args){
  12.                 Person p1 = new Person(20);
  13.                 boolean b = p1.compare(new Person(20));
  14.                 System.out.println(b);
  15.         }
  16. }
复制代码


回复 使用道具 举报
  1. //构造函数之间的调用,使用this
  2. class Person{
  3.         private String name;
  4.         private int age;
  5.         Person(String name){
  6.                 this.name = name;
  7.         }
  8.         Person(String name,int age){
  9.                 this(name);//本类构造函数间调用,必须这么写
  10.                 this.age = age;
  11.         }
  12. }
  13. //Person p = new Person("lisi",30);
  14. //执行过程:this("lisi");p("lisi");new Person("lisi");
  15. /*
  16. 注意:this();只能定义在构造函数的第一行,由于构造函数是初始化动作,相对于外层构造函数的一般操作动作,
  17.    初始化必须先执行。
  18.    构造函数间不允许循环调用(A调用B,B调用A)
  19.    一般函数不能调用构造函数
  20. */
复制代码


回复 使用道具 举报
  1. 类上注释
  2. /**
  3. *类的介绍
  4. *@author 作者
  5. *@version 版本
  6. */
  7. 方法上注释(public protected)其他权限不会被制作帮助文档
  8. /**
  9. * 功能简介
  10. * @param 参数 参数介绍 可以写多个,一行一个
  11. * @return 返回值介绍
  12. */
复制代码


回复 使用道具 举报
学习了 谢谢
回复 使用道具 举报
  1. //饿汉式
  2. class Single{
  3.         private static Single s = new Single();
  4.         private Single(){}
  5.         public static Single getInstance(){
  6.                 return s;
  7.         }
  8. }
复制代码
  1. //懒汉式
  2. class Single{
  3.         private static Single s = null;
  4.         private Single(){}
  5.         public static Single getInstance(){
  6.                 if(s==null){
  7.                         synchronized (Single.class){
  8.                                 if(s==null){
  9.                                         s = new Single();
  10.                                 }
  11.                         }
  12.                 }
  13.                 return s;
  14.         }
  15. }
复制代码


回复 使用道具 举报
  1. //获取当前时间:System.currentTimeMillis();返回long 时间毫秒与1970年1月1日午夜的时间差
  2. abstract class GetTime{
  3.         public abstract void run();
  4.         public final void getTime(){
  5.                 long start = System.currentTimeMillis();
  6.                 run();
  7.                 long end = System.currentTimeMillis();
  8.                 System.out.println("毫秒:"+(end-start));
  9.         }
  10. }
  11. //模版方法模式:定义功能时,一部分是确定的,另一部分是不确定的,并且确定
  12. //的部分在使用不确定的部分,把不确定的部分暴露出去,让子类去实现
复制代码


回复 使用道具 举报
  1. //接口:降低耦合,提高扩展(子类实现方法),定义规则
  2. //多态:提高扩展(符合规则的类都可以纳入体系)
  3. //多态实例:主体调用接口
  4. interface PCI{
  5.         public void open();
  6.         public void close();
  7. }
  8. class MainBoard{
  9.         public void run(){
  10.                 System.out.println("mainboard run");
  11.         }
  12.         public void usePCI(PCI p){
  13.                 //if(p!=null)
  14.                 p.open();
  15.                 p.close();
  16.         }
  17. }
  18. class NetCard implements PCI{
  19.         public void open() {
  20.                 System.out.println("netcard open");
  21.         }
  22.         public void close() {
  23.                 System.out.println("netcard close");
  24.         }       
  25. }
复制代码


回复 使用道具 举报
  1. abstract class AbsDemo{
  2.         abstract void show();
  3. }
  4. class Outer{
  5.         int x = 3;
  6.         public void function(){
  7.                 new AbsDemo(){
  8.                         void show(){
  9.                                 System.out.println(x);
  10.                         }
  11.                 }.show();
  12.         }
  13. }
复制代码


回复 使用道具 举报
  1. class Demo{
  2.         int div(int a,int b){
  3.                 return a/b;//2、出现异常并被JVM识别,封装为AritchmeticException 3、抛给main函数
  4.         }
  5. }
  6. class ExceptionDemo{
  7.         public static void main(String[] args){
  8.                 Demo d = new Demo();
  9.                 try{//4、被try检测到(没有try时,main函数直接将问题交给JVM,调用异常处理机制)
  10.                         int x = d.div(4, 0);//1、参数传给形参进行运算
  11.                         System.out.println(x);//不执行
  12.                 }catch(Exception e){//5、将异常交给catch捕获,被catch形参接收
  13.                         System.out.println(".....");//6、执行处理代码,try...catch代码块到此结束
  14.                 }
  15.                 System.out.println("over");//7、继续向下执行
  16.         }
  17. }
复制代码


回复 使用道具 举报
  1. private String msg;//Throwable中已经有一个private String message;并提供了getter以及构造函数赋值方式
  2. xxxException(String msg){//xxxException(String msg){
  3.         this.msg = msg;      //     super(msg);
  4. }                        //}即可,除此之外,自定义异常类中可以定义自己的方法,特有数据
  5. public String getMessage(){
  6.         return msg;
  7. }
  8. //toString 内部调用getMessage
复制代码


回复 使用道具 举报
  1. /*
  2. *售票:多窗口同时售票
  3. */
  4. class Ticket extends Thread{
  5.         private static int t = 100;
  6.         public void run(){
  7.                 while(true){
  8.                         if(t>0){
  9.                                 System.out.println(Thread.currentThread().getName()+"...sale:"+t--);
  10.                         }
  11.                 }
  12.         }
  13. }
  14. class TicketDemo{
  15.         public static void main(String[] args) {
  16.                 Ticket t1 = new Ticket();
  17.                 Ticket t2 = new Ticket();
  18.                 t1.start();
  19.                 t2.start();
  20.         }
  21. }
  22. /*
  23. *问题:
  24. *    1、线程间数据共享,使用静态解决,不推荐静态,静态的生命周期太长
  25. *    2、 票不按数序出售:单核不会出现,多核时,会出现1号票出售,但未打印前,2号票同时完成出售+打印,之后1号票才打印的情况
  26. *    3、当同一个线程被多次启动时,会抛出IllegalThreadStateException:无效的线程状态异常。stat只针对线程创建状态
  27. */
复制代码


回复 使用道具 举报
  1. //模拟线程安全问题
  2. class Ticket implements Runnable{
  3.         private int t = 100;
  4.         public void run() {
  5.                 while(true){
  6.                         if(t>0){
  7.                                 try {
  8.                                         Thread.sleep(100);
  9.                                 } catch (InterruptedException e) {
  10.                                         e.printStackTrace();
  11.                                 }
  12.                                 System.out.println(t--);
  13.                         }
  14.                 }
  15.         }
  16.        
  17. }
  18. /*
  19. *注意:run方法不能抛出异常,因为run方法是继承来的,其父类/接口中的run方法未抛出任何异常,所以必须try
  20. *  多线程必须要十分小心安全问题,因为测试时可能测不出来
  21. *问题:多条语句操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完,
  22. *  另一个线程参与进来执行,导致共享数据的错误
  23. *解决办法:对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,其他线程不可以参与执行
  24. */
复制代码


回复 使用道具 举报
  1. class Bank{
  2.         private int sum;
  3.         public synchronized void add(int n){
  4.                 sum = sum + n;
  5.                 System.out.println(sum);
  6.         }
  7. }
  8. class Cus implements Runnable{
  9.         private Bank b = new Bank();
  10.         public void run(){
  11.                 for(int x=0;x<3;x++){
  12.                         b.add(100);
  13.                 }
  14.         }
  15. }
  16. /*
  17. * 判断是否需要同步:1、哪些是多线程 2、是否共享数据 3、多线程哪些语句在操作共享数据
  18. * 本例子中,run方法中的语句有操作共享数据,调用add方法,所以add方法需要同步。
  19. * 此时不要在run中同步,run中同步,相当于单线程,要在add中同步。
  20. */
复制代码


回复 使用道具 举报
  1. //验证:两个线程,一个同步代码块(锁为其他则二线程不同步,锁为this则同步),一个同步函数。
  2. //实现:两个线程执行不同代码,定义一个flag,true执行一部分run,false执行另一部分run
  3. class Ticket implements Runnable{
  4.         private int tick = 100;
  5.         Object obj = new Object();
  6.         boolean flag = true;
  7.         public void run(){
  8.                 if(flag){
  9.                         while(true){
  10.                                 synchronized (obj) {//当锁为this时,同步
  11.                                         if(tick>0){
  12.                                                 try {
  13.                                                         Thread.sleep(100);
  14.                                                 } catch (InterruptedException e) {
  15.                                                         e.printStackTrace();
  16.                                                 }
  17.                                                 System.out.println(Thread.currentThread().getName()+"..code:"+tick--);
  18.                                         }
  19.                                 }
  20.                         }
  21.                 }else{
  22.                         while(true){
  23.                                 show();
  24.                         }
  25.                 }
  26.         }
  27.         public synchronized void show(){
  28.                 if(tick>0){
  29.                         try {
  30.                                 Thread.sleep(100);
  31.                         } catch (InterruptedException e) {
  32.                                 e.printStackTrace();
  33.                         }
  34.                         System.out.println(Thread.currentThread().getName()+"..show:"+tick--);
  35.                 }
  36.         }
  37. }
  38. class ThisLockDemo{
  39.         public static void main(String[] args) {
  40.                 Ticket t = new Ticket();
  41.                 Thread t1 = new Thread(t);
  42.                 Thread t2 = new Thread(t);
  43.                 t1.start();
  44.                 try {
  45.                         Thread.sleep(100);//防止t1启动后不执行,直接改变flag,造成混乱
  46.                 } catch (InterruptedException e) {
  47.                         e.printStackTrace();
  48.                 }
  49.                 t.flag = false;
  50.                 t2.start();
  51.         }
  52. }
复制代码


回复 使用道具 举报
  1. class Single{
  2.         private static Single s = null;
  3.         private Single(){}
  4.         public static Single getInstance(){//使用同步函数低效
  5.                 if(s==null){
  6.                         synchronized (Single.class) {//静态锁
  7.                                 if(s==null){
  8.                                         s = new Single();
  9.                                 }
  10.                         }
  11.                 }
  12.                 return s;
  13.         }
  14. }
复制代码


回复 使用道具 举报
  1. class SynAddRunnable implements Runnable{
  2.         int a,b;
  3.         public SynAddRunnable(int a,int b){
  4.                 this.a = a;
  5.                 this.b = b;
  6.         }
  7.         public void run(){
  8.                 synchronized (Integer.valueOf(a)) {
  9.                         synchronized (Integer.valueOf(b)) {//Integer.valueOf为了节省资源,同一个数字只创建一个对象
  10.                                 System.out.println(a+b);
  11.                         }
  12.                 }
  13.         }
  14.         public static void main(String[] args) {
  15.                 for(int x=0;x<100;x++){
  16.                         new Thread(new SynAddRunnable(1, 2)).start();
  17.                         new Thread(new SynAddRunnable(2, 1)).start();
  18.                 }
  19.         }
  20. }
复制代码


回复 使用道具 举报
  1. class Ticket implements Runnable{
  2.         private int tick = 100;
  3.         Object obj = new Object();
  4.         boolean flag = true;
  5.         public void run(){
  6.                 if(flag){
  7.                         while(true){
  8.                                 synchronized (obj) {//代码块中有函数中有代码块
  9.                                         show();
  10.                                 }
  11.                         }
  12.                 }else{
  13.                         while(true){
  14.                                 show();
  15.                         }
  16.                 }
  17.         }
  18.         public synchronized void show(){
  19.                 synchronized (obj) {
  20.                         if(tick>0){
  21.                                 try {
  22.                                         Thread.sleep(100);
  23.                                 } catch (InterruptedException e) {
  24.                                         e.printStackTrace();
  25.                                 }
  26.                                 System.out.println(Thread.currentThread().getName()+"...code:"+tick--);
  27.                         }
  28.                 }
  29.         }
  30. }
  31. class DeadLockDemo{
  32.         public static void main(String[] args) {
  33.                 Ticket t = new Ticket();
  34.                 Thread t1 = new Thread(t);
  35.                 Thread t2 = new Thread(t);
  36.                 t1.start();
  37.                 try {
  38.                         Thread.sleep(100);
  39.                 } catch (InterruptedException e) {
  40.                         e.printStackTrace();
  41.                 }
  42.                 t.flag = false;
  43.                 t2.start();
  44.         }
  45. }
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马