Java基础&算法

文章目录

          • int 转short
          • int 转byte数组
          • byte 转int 数组
          • 加锁单例
          • & | ^ ~
          • 线性链表 LinkedList
          • 并发线性队列 ConcurrentLinkedQueue
          • 轮训线程

int 转short
  public static short[] convertToShort(int i)   
    {   
        short[] a=new short[2];   
        a[0] = (short) (i & 0x0000ffff);          //将整型的低位取出,   
        a[1] = (short) (i >> 16);                     //将整型的高位取出.   
        return a;   
    }   
      
    public static int convertToInt(short[] a)   
    {   
        return ((a[1]<<16))|(a[0]&0x0000ffff);  //做&操作可以保证转化后的数据长度保持16位!   
    }   

PS:0x 表示16进制(0000为一个F;0xFF为8位;0xFFFF位16位;0x0000FFFF为32位)

  1. >> 右移 >>16
    1 1111 1111 1111 1111
    1 1111 1111 1111 1111
    多余部分删除
    1
    物理右移 得1
    2.& 只要都是1 就不置0 起到过滤作用,i & 0x0000ffff 即过滤高16位
    3.| 不过滤,起合并作用,
    硬算
    二进制除法
    (272 / 6)=> (1011 1010 1010/0000 0110)
    Java基础&算法_第1张图片
    讲解很详细
    Java中&0xFF是什么意思
int 转byte数组
    public static byte[] int2byteArray(int value) {
        byte[] src = new byte[4];
        // 由低位到高位
        src[0] = (byte) (value & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[3] = (byte) ((value >> 24) & 0xFF);
        return src;
    }
byte 转int 数组
    public static byte[] int2byteArray2(int value) {
        byte[] src = new byte[4];
        // 由高位到低位
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }
加锁单例

缩小锁范围,避免死锁,外层判断避免无意义操作。

if (mInstance == null)
        {
            synchronized (OkHttpUtils.class)
            {
                if (mInstance == null)
                {
                    mInstance = new OkHttpUtils(okHttpClient);
                }
            }
        }
& | ^ ~

https://www.cnblogs.com/yesiamhere/p/6675067.html

线性链表 LinkedList

LinkedList

并发线性队列 ConcurrentLinkedQueue

ConcurrentLinkedQueue
建议直接官网查看Google

轮训线程

public class FrameThread implements Runnable{

	private Thread thread;
	
	private static int i = 0;
	
	private Interface interfaceParams;
	private int sleepTimes = 1000;
	
	public FrameThread(GatherY16Interface interfaceParams) {
		this.interfaceParams = interfaceParams;
	}
	
	public void setSleepTime(int sleepTimes){
		this.sleepTimes = sleepTimes;
	}
	
	public void startThrad() {

		thread = new Thread(this);
		
		i++;
		
		thread.setName("GuideThread" + i);
		
		thread.start();
	}
	
	public void closeThread() {
		
		if(thread != null){
			thread.interrupt();
			
			thread = null;
		}

	}
	
	
	public Thread getThread() {
		return thread;
	}
	
	
	@Override
	public void run() {

		try {
			while (thread != null && !thread.isInterrupted()) {

				long startTime = System.currentTimeMillis();

				interfaceParams.gatherY16();

				long endTime = System.currentTimeMillis();

				int userTime = (int) (endTime - startTime);

				if (userTime < sleepTimes) {
					Thread.sleep(sleepTimes - userTime);
				}
			}

		} catch (Exception e) {
			
			Log.v(MainApp.TAG,e.toString());
			
			if(thread != null){
				thread.isInterrupted();
			}
		} finally {
			thread = null;
		}

	}
		

}

你可能感兴趣的:(Java基础)