System.out.println(a + "\n"+ b + "\n" + ("%.6f",c));
时间: 2023-09-29 08:08:44 浏览: 50
这段代码有误,应该改为:
System.out.println(a + "\n"+ b + "\n" + String.format("%.6f",c));
这段代码的作用是输出三个变量a、b、c的值,其中c保留小数点后六位。具体来说,"\n"表示换行符,用来将a、b、c的值分行输出;String.format("%.6f",c)表示将变量c格式化为保留小数点后六位的字符串,然后将该字符串与a和b的值拼接起来输出。
相关问题
String formattedNumber=String.format("%.6f",sum);
这段代码是Java中的字符串格式化操作,将一个浮点数sum格式化为小数点后6位的字符串formattedNumber。其中,"%.6f"是格式化字符串的格式,其中%表示占位符,.6表示小数点后6位,f表示浮点数类型。这个方法可以用于将浮点数转换为字符串并控制小数点后的位数。
```java
double sum = 3.1415926;
String formattedNumber = String.format("%.6f", sum);
System.out.println(formattedNumber); // 输出:3.141593
```
java.util.concurrent.ThreadPoolExecutor 测试 main
以下是一个使用java.util.concurrent.ThreadPoolExecutor测试的例子:
```java
import java.util.concurrent.*;
public class TestThreadPoolExecutor {
public static void main(String[] args) {
int corePoolSize = 2;
int maxPoolSize = 4;
long keepAliveTime = 10;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue);
executor.execute(new Task("任务1"));
executor.execute(new Task("任务2"));
executor.execute(new Task("任务3"));
executor.execute(new Task("任务4"));
executor.execute(new Task("任务5"));
executor.shutdown();
}
static class Task implements Runnable {
private String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 正在执行 " + name);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 执行 " + name + " 完成");
}
}
}
```
输出结果:
```
pool-1-thread-1 正在执行 任务1
pool-1-thread-2 正在执行 任务2
pool-1-thread-1 执行 任务1 完成
pool-1-thread-2 执行 任务2 完成
pool-1-thread-3 正在执行 任务3
pool-1-thread-4 正在执行 任务4
pool-1-thread-3 执行 任务3 完成
pool-1-thread-4 执行 任务4 完成
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task TestThreadPoolExecutor$Task@7f31245a rejected from java.util.concurrent.ThreadPoolExecutor@6d6f6e28[Running, pool size = 4, active threads = 4, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at TestThreadPoolExecutor.main(TestThreadPoolExecutor.java:16)
```
在这个例子中,我们创建了一个ThreadPoolExecutor对象,并向其中添加了5个任务。由于线程池的核心线程数为2,最大线程数为4,所以前两个任务会立即执行,而后面的三个任务会被放入阻塞队列中等待执行。由于阻塞队列的大小为2,所以当第5个任务被添加时,阻塞队列已满,此时线程池会创建新的线程来执行任务。由于线程池的最大线程数为4,所以当第6个任务被添加时,线程池已经达到最大线程数,此时会抛出RejectedExecutionException异常。最后,我们调用了线程池的shutdown()方法来关闭线程池。