gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
Write a java program that runs two threads, each one of them prints on the screen a…
- User
Write a java program that runs two threads, each one of them prints on the screen a counter value (which starts from 0 until a20) associated with its thread ID. Print the output screen which will have • counter/threadID.
- Assistant
Here's the Java program that runs two threads, with each thread printing its own counter value and thread ID:
codepublic class ThreadExample implements Runnable { private int threadID; private static final int MAX_COUNT = 20; public ThreadExample(int threadID) { this.threadID = threadID; } @Override public void run() { for(int i = 0; i <= MAX_COUNT; i++) { System.out.println(i + "/" + threadID); try { Thread.sleep(100); // Sleep for 0.1 seconds } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Thread t1 = new Thread(new ThreadExample(1)); Thread t2 = new Thread(new ThreadExample(2)); t1.start(); t2.start(); } }