/** * This "helper class" implements the timer's task execution thread, which * waits for tasks on the timer queue, executions them when they fire, * reschedules repeating tasks, and removes cancelled tasks and spent * non-repeating tasks from the queue. */ class TimerThread extends Thread { /** * This flag is set to false by the reaper to inform us that there * are no more live references to our Timer object. Once this flag * is true and there are no more tasks in our queue, there is no * work left for us to do, so we terminate gracefully. Note that * this field is protected by queue's monitor! */ boolean newTasksMayBeScheduled = true; /** * Our Timer's queue. We store this reference in preference to * a reference to the Timer so the reference graph remains acyclic. * Otherwise, the Timer would never be garbage-collected and this * thread would never go away. */ private TaskQueue queue; TimerThread(TaskQueue queue) { this.queue = queue; } public void run() { try { mainLoop(); } finally { // Someone killed this Thread, behave as if Timer cancelled synchronized(queue) { newTasksMayBeScheduled = false; queue.clear(); // Eliminate obsolete references } } } /** * The main timer loop. (See class comment.) */ private void mainLoop() { while (true) { try { TimerTask task; boolean taskFired; synchronized(queue) { // Wait for queue to become non-empty while (queue.isEmpty() && newTasksMayBeScheduled) queue.wait(); if (queue.isEmpty()) break; // Queue is empty and will forever remain; die // Queue nonempty; look at first evt and do the right thing long currentTime, executionTime; task = queue.getMin(); synchronized(task.lock) { if (task.state == TimerTask.CANCELLED) { queue.removeMin(); continue; // No action required, poll queue again } currentTime = System.currentTimeMillis(); executionTime = task.nextExecutionTime; if (taskFired = (executionTime<=currentTime)) { if (task.period == 0) { // Non-repeating, remove queue.removeMin(); task.state = TimerTask.EXECUTED; } else { // Repeating task, reschedule queue.rescheduleMin( task.period<0 ? currentTime - task.period : executionTime + task.period); } } } if (!taskFired) // Task hasn't yet fired; wait queue.wait(executionTime - currentTime); } if (taskFired) // Task fired; run it, holding no locks task.run(); } catch(InterruptedException e) { } } } }
just for fun. 吃饱了没事干,玩呗!
replaceAll也是循环实现,so...public String replaceAll(String replacement) { reset(); boolean result = find(); if (result) { StringBuffer sb = new StringBuffer(); do { appendReplacement(sb, replacement); result = find(); } while (result); appendTail(sb); return sb.toString(); } return text.toString(); }
那你上一张自己的PP吧。看你空间是个MM?
再来一个 java代码public class Hello{ public static void main(String[] args){ System.out.println("hello"); } }再在Hello.java旁边兴建一个文本命名为run.cmd里面写上一下内容 @echo off javac Hello.java set var=0 :b call java Hello set /a var+=1 if %var% lss 10 goto b pause没有循环了吧
int num=4;
Object[] obj=new Object[num];
Arrays.fill(obj,str);
String s=Arrays.toString(obj);
s=s.replaceAll("([\\[\\]]|[,][\\s])","\n");
System.out.println(s);
你挖,我也挖
javas2高人的回答
System.out.println("hello");
System.out.println("hello");
...
有多少打多少吧,嘿嘿
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){ @Override
public void run() {
System.out.println("Hello world");
}
}, 0, 1000);
}
public static void main(String[] args) throws Exception {
new Thread(new Printer(10)).start();
}
}class Printer implements Runnable {
int i; Printer(int i) {
this.i = i;
} public void run() {
if (i > 0) {
System.out.println("hello"); new Thread(new Printer(i - 1)).start();
}
}
}
有Timer不用,还自己写线程
那个是开玩笑,因为你的题目不严谨,没说循环、递归到哪里为止。比如native的不算。另外,你那个Printer 其实也是递归
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){ @Override
public void run() {
System.out.println("Hello world");
}
}, 0, 1000);
}
java.util.TimerThread.mainLoop方法
/**
* This "helper class" implements the timer's task execution thread, which
* waits for tasks on the timer queue, executions them when they fire,
* reschedules repeating tasks, and removes cancelled tasks and spent
* non-repeating tasks from the queue.
*/
class TimerThread extends Thread {
/**
* This flag is set to false by the reaper to inform us that there
* are no more live references to our Timer object. Once this flag
* is true and there are no more tasks in our queue, there is no
* work left for us to do, so we terminate gracefully. Note that
* this field is protected by queue's monitor!
*/
boolean newTasksMayBeScheduled = true; /**
* Our Timer's queue. We store this reference in preference to
* a reference to the Timer so the reference graph remains acyclic.
* Otherwise, the Timer would never be garbage-collected and this
* thread would never go away.
*/
private TaskQueue queue; TimerThread(TaskQueue queue) {
this.queue = queue;
} public void run() {
try {
mainLoop();
} finally {
// Someone killed this Thread, behave as if Timer cancelled
synchronized(queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
} /**
* The main timer loop. (See class comment.)
*/
private void mainLoop() {
while (true) {
try {
TimerTask task;
boolean taskFired;
synchronized(queue) {
// Wait for queue to become non-empty
while (queue.isEmpty() && newTasksMayBeScheduled)
queue.wait();
if (queue.isEmpty())
break; // Queue is empty and will forever remain; die // Queue nonempty; look at first evt and do the right thing
long currentTime, executionTime;
task = queue.getMin();
synchronized(task.lock) {
if (task.state == TimerTask.CANCELLED) {
queue.removeMin();
continue; // No action required, poll queue again
}
currentTime = System.currentTimeMillis();
executionTime = task.nextExecutionTime;
if (taskFired = (executionTime<=currentTime)) {
if (task.period == 0) { // Non-repeating, remove
queue.removeMin();
task.state = TimerTask.EXECUTED;
} else { // Repeating task, reschedule
queue.rescheduleMin(
task.period<0 ? currentTime - task.period
: executionTime + task.period);
}
}
}
if (!taskFired) // Task hasn't yet fired; wait
queue.wait(executionTime - currentTime);
}
if (taskFired) // Task fired; run it, holding no locks
task.run();
} catch(InterruptedException e) {
}
}
}
}
reset();
boolean result = find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
appendReplacement(sb, replacement);
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
java代码public class Hello{
public static void main(String[] args){
System.out.println("hello");
}
}再在Hello.java旁边兴建一个文本命名为run.cmd里面写上一下内容
@echo off
javac Hello.java
set var=0
:b
call java Hello
set /a var+=1
if %var% lss 10 goto b
pause没有循环了吧
int 最大2^31 - 1,hello\r\n正好8个字符2^3 31-3=28,可惜差一,所以只能27次。这个代码的重复是有限次的,你n=100w也无所谓。
int n = 6;
String hello = "hello\r\n";
String str = hello;
str = str.concat(str); // 1
str = str.concat(str); // 2
str = str.concat(str); // 3
str = str.concat(str); // 4
str = str.concat(str); // 5
str = str.concat(str); // 6
str = str.concat(str); // 7
str = str.concat(str); // 8
str = str.concat(str); // 9
str = str.concat(str); // 10
str = str.concat(str); // 11
str = str.concat(str); // 12
str = str.concat(str); // 13
str = str.concat(str); // 14
str = str.concat(str); // 15
str = str.concat(str); // 16 再大的话内存恐怕不够了
System.out.println(str.length());
System.out.println(str.substring(0, hello.length() * n));
想过的方案包括:
1 Formatter %ns + replace
2 Thread.UncaughtExceptionHandler,用异常,可惜这家伙你再抛出异常就不会自己handle了
3 char[n] System.arraycopy直到越界,由这个想到上面方案
4 文件复制,没考虑周全
5 管道,和文件道理差不多小绵羊的bat早想过了,类似无耻方案还包括:
1 jni -> c goto
2 Http重定向 将?x=5重定向到?x=4
void print()
{
System.out.println("hello");
++count ;
if(count < max)
print();
}
口中默数,100,99,98,97,96,95...5,4,3,2,1
手中默点,数一下就双击一下程序,数一下就双击一下程序...
发广告让大家点,点一次出一个hello,点一次出一个hello
直到达到指定数字
这个方案俗称云计算...
public static void main(String[] args) {
System.out.println("n个 hello (n>=1) 。");
}
洗澡时候想到的
public class Test {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
java Test "hello hello hello"
int printhello(int n)
{static int m = 1;
if(m++!=n)
printf("hello\n");
if(n==1)
{
printf("hello\n");
return 1;
}
if(n<=0)
return 0;
printhello(n-1);
return 1;
}
main()
{
printhello(5);
}