一分钟学会java System.arrayCopy 方法

System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制

本文主要介绍这个方法的使用说明


代码:

下面为此方法的代码:

public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);

参数说明:

Object src : 原数组
int srcPos : 从元数据的起始位置开始
Object dest : 目标数组
int destPos : 目标数组的开始起始位置
int length : 要copy的数组的长度


demo:

byte[] srcBytes = new byte[]{1,2,3,4,5,6};    // 原数组
byte[] destBytes = new byte[6];   // 目标数组

//使用System.arraycopy进行转换
System.arraycopy(srcBytes,0, destBytes ,0,6);

上面这段代码就是 : 创建一个一维空数组,数组的总长度为 6位,然后将srcBytes源数组中 从0位 到 第5位之间的数值 copy 到 destBytes目标数组中,在目标数组的第0位开始放置.

分析一下这个里面的sleep函数,是什么意思// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package cn.hutool.core.thread; import cn.hutool.core.util.RuntimeUtil; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; public class ThreadUtil { public ThreadUtil() { } public static ExecutorService newExecutor(int corePoolSize) { ExecutorBuilder builder = ExecutorBuilder.create(); if (corePoolSize > 0) { builder.setCorePoolSize(corePoolSize); } return builder.build(); } public static ExecutorService newExecutor() { return ExecutorBuilder.create().useSynchronousQueue().build(); } public static ExecutorService newSingleExecutor() { return ExecutorBuilder.create().setCorePoolSize(1).setMaxPoolSize(1).setKeepAliveTime(0L).buildFinalizable(); } public static ThreadPoolExecutor newExecutor(int corePoolSize, int maximumPoolSize) { return ExecutorBuilder.create().setCorePoolSize(corePoolSize).setMaxPoolSize(maximumPoolSize).build(); } public static ExecutorService newExecutor(int corePoolSize, int maximumPoolSize, int maximumQueueSize) { return ExecutorBuilder.create().setCorePoolSize(corePoolSize).setMaxPoolSize(maximumPoolSize).setWorkQueue(new LinkedBlockingQueue(maximumQueueSize)).build(); } public static ThreadPoolExecutor newExecutorByBlockingCoefficient(float blockingCoefficient) { if (!(blockingCoefficient >= 1.0F) && !(blockingCoefficient < 0.0F)) { int poolSize = (int)((float)RuntimeUtil.getProcessorCount() / (1.0F - blockingCoefficient)); return ExecutorBuilder.create().setCorePoolSize(poolSize).setMaxPoolSize(poolSize).setKeepAliveTime(0L).build(); } else { throw new IllegalArgumentException("[blockingCoefficient] must between 0 and 1, or equals 0."); } } public static ExecutorService newFixedExecutor(int nThreads, String threadNamePrefix, boolean isBlocked) { return newFixedExecutor(nThreads, 1024, threadNamePrefix, isBlocked); } public static ExecutorService newFixedExecutor(int nThreads, int maximumQueueSize, String threadNamePrefix, boolean isBlocked) { return newFixedExecutor(nThreads, maximumQueueSize, threadNamePrefix, (isBlocked ? RejectPolicy.BLOCK : RejectPolicy.ABORT).getValue()); } public static ExecutorService newFixedExecutor(int nThreads, int maximumQueueSize, String threadNamePrefix, RejectedExecutionHandler handler) { return ExecutorBuilder.create().setCorePoolSize(nThreads).setMaxPoolSize(nThreads).setWorkQueue(new LinkedBlockingQueue(maximumQueueSize)).setThreadFactory(createThreadFactory(threadNamePrefix)).setHandler(handler).build(); } public static void execute(Runnable runnable) { GlobalThreadPool.execute(runnable); } public static Runnable execAsync(Runnable runnable, boolean isDaemon) { Thread thread = new Thread(runnable); thread.setDaemon(isDaemon); thread.start(); return runnable; } public static <T> Future<T> execAsync(Callable<T> task) { return GlobalThreadPool.submit(task); } public static Future<?> execAsync(Runnable runnable) { return GlobalThreadPool.submit(runnable); } public static <T> CompletionService<T> newCompletionService() { return new ExecutorCompletionService(GlobalThreadPool.getExecutor()); } public static <T> CompletionService<T> newCompletionService(ExecutorService executor) { return new ExecutorCompletionService(executor); } public static CountDownLatch newCountDownLatch(int threadCount) { return new CountDownLatch(threadCount); } public static Thread newThread(Runnable runnable, String name) { Thread t = newThread(runnable, name, false); if (t.getPriority() != 5) { t.setPriority(5); } return t; } public static Thread newThread(Runnable runnable, String name, boolean isDaemon) { Thread t = new Thread((ThreadGroup)null, runnable, name); t.setDaemon(isDaemon); return t; } public static boolean sleep(Number timeout, TimeUnit timeUnit) { try { timeUnit.sleep(timeout.longValue()); return true; } catch (InterruptedException var3) { return false; } } public static boolean sleep(Number millis) { return millis == null ? true : sleep(millis.longValue()); } public static boolean sleep(long millis) { if (millis > 0L) { try { Thread.sleep(millis); } catch (InterruptedException var3) { return false; } } return true; } public static boolean safeSleep(Number millis) { return millis == null ? true : safeSleep(millis.longValue()); } public static boolean safeSleep(long millis) { long spendTime; for(long done = 0L; done >= 0L && done < millis; done += spendTime) { long before = System.currentTimeMillis(); if (!sleep(millis - done)) { return false; } spendTime = System.currentTimeMillis() - before; if (spendTime <= 0L) { break; } } return true; } public static StackTraceElement[] getStackTrace() { return Thread.currentThread().getStackTrace(); } public static StackTraceElement getStackTraceElement(int i) { StackTraceElement[] stackTrace = getStackTrace(); if (i < 0) { i += stackTrace.length; } return stackTrace[i]; } public static <T> ThreadLocal<T> createThreadLocal(boolean isInheritable) { return (ThreadLocal<T>)(isInheritable ? new InheritableThreadLocal() : new ThreadLocal()); } public static <T> ThreadLocal<T> createThreadLocal(Supplier<? extends T> supplier) { return ThreadLocal.withInitial(supplier); } public static ThreadFactoryBuilder createThreadFactoryBuilder() { return ThreadFactoryBuilder.create(); } public static ThreadFactory createThreadFactory(String threadNamePrefix) { return ThreadFactoryBuilder.create().setNamePrefix(threadNamePrefix).build(); } public static void interrupt(Thread thread, boolean isJoin) { if (null != thread && !thread.isInterrupted()) { thread.interrupt(); if (isJoin) { waitForDie(thread); } } } public static void waitForDie() { waitForDie(Thread.currentThread()); } public static void waitForDie(Thread thread) { if (null != thread) { boolean dead = false; do { try { thread.join(); dead = true; } catch (InterruptedException var3) { } } while(!dead); } } public static Thread[] getThreads() { return getThreads(Thread.currentThread().getThreadGroup().getParent()); } public static Thread[] getThreads(ThreadGroup group) { Thread[] slackList = new Thread[group.activeCount() * 2]; int actualSize = group.enumerate(slackList); Thread[] result = new Thread[actualSize]; System.arraycopy(slackList, 0, result, 0, actualSize); return result; } public static Thread getMainThread() { for(Thread thread : getThreads()) { if (thread.getId() == 1L) { return thread; } } return null; } public static ThreadGroup currentThreadGroup() { SecurityManager s = System.getSecurityManager(); return null != s ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); } public static ThreadFactory newNamedThreadFactory(String prefix, boolean isDaemon) { return new NamedThreadFactory(prefix, isDaemon); } public static ThreadFactory newNamedThreadFactory(String prefix, ThreadGroup threadGroup, boolean isDaemon) { return new NamedThreadFactory(prefix, threadGroup, isDaemon); } public static ThreadFactory newNamedThreadFactory(String prefix, ThreadGroup threadGroup, boolean isDaemon, Thread.UncaughtExceptionHandler handler) { return new NamedThreadFactory(prefix, threadGroup, isDaemon, handler); } public static void sync(Object obj) { synchronized(obj) { try { obj.wait(); } catch (InterruptedException var4) { } } } public static ConcurrencyTester concurrencyTest(int threadSize, Runnable runnable) { return (new ConcurrencyTester(threadSize)).test(runnable); } public static ScheduledThreadPoolExecutor createScheduledExecutor(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public static ScheduledThreadPoolExecutor schedule(ScheduledThreadPoolExecutor executor, Runnable command, long initialDelay, long period, boolean fixedRateOrFixedDelay) { return schedule(executor, command, initialDelay, period, TimeUnit.MILLISECONDS, fixedRateOrFixedDelay); } public static ScheduledThreadPoolExecutor schedule(ScheduledThreadPoolExecutor executor, Runnable command, long initialDelay, long period, TimeUnit timeUnit, boolean fixedRateOrFixedDelay) { if (null == executor) { executor = createScheduledExecutor(2); } if (fixedRateOrFixedDelay) { executor.scheduleAtFixedRate(command, initialDelay, period, timeUnit); } else { executor.scheduleWithFixedDelay(command, initialDelay, period, timeUnit); } return executor; } }
最新发布
11-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值