static int k = 0; final static int kmax = 1000000; synchronized static int getK() { if (k < kmax) { int bk = k; k++; return bk; } return -1; } public static void main(String[] args) { int threadMax = 10; for (int j = 0; j < threadMax; j++) { final Thread thr = new Thread("tname:" + j) { @Override public void run() { int i; while ((i = getK()) != -1) { System.out.println(Thread.currentThread() + "\tvalue:" + i); try { Thread.sleep(0); } catch (InterruptedException ex) { } } } }; thr.setDaemon(true); thr.start(); } Scanner sc = new Scanner(System.in); sc.nextLine(); }
//方法很多,我采取最简单的方法。还有1000000特别大,如果屏幕显示不完,打印到文件才能看出结果,或者把它的值变小 import java.util.concurrent.*; public class Counter implements Runnable { public static volatile int i = 0; private static final int SIZE = 1000;//1000000;太 public static synchronized void next() { if (i < SIZE) System.out.println("value:" + i++); } public void run() { while (i < 10000) next();
} public static void main(String[] args) throws Exception { ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < 20; i++) { exec.execute(new Counter()); } } }
main方法中 我设置的20 表示有20个线程同时运行
package com.j913;public class Test { public static void main(String[] args) { Print p = new Print(); Thread t1 = new Thread(p); Thread t2 = new Thread(p); Thread t3 = new Thread(p); t1.start(); t2.start(); t3.start(); } }class Print implements Runnable { private int i; @Override public void run() { synchronized (this) { for (; i < 10000; i++) { System.out.println("thread:" + Thread.currentThread().getName() + " value:" + i); } } }}是这个意思吗,几个线程合作打印出结果。
拆分成几个任务,每个任务执行一部分就行了import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;public class TestMultiThread { public static void main(String[] args) throws InterruptedException, ExecutionException { int amount = 10000; int part = 1000; int start = 0; int end = 0;
ExecutorService threadPool = Executors.newCachedThreadPool(); Collection<Callable<String>> tasks = new ArrayList<Callable<String>>(); for(int i = 1; i <= amount/part; i++){ end = start + part; tasks.add(new DisplayValue(start, end)); start = end; }
for(Future<String> result : results){ System.out.println(result.get()); } }}class DisplayValue implements Callable<String>{ private int start = 0; private int end = 0; StringBuilder sb = new StringBuilder();
public String call() { for(int i = start; i < end; i++){ // sb.append("value : "); sb.append(i); sb.append(" "); if(i > start && i % 100 == 0){ sb.append("\n"); } } sb.append("\n"); return sb.toString(); } }
如果不需要按顺序执行的话,可以每个线程打印某个位置的数, 如第一个线程打印0,5,10... 第二个打印1,6,11... 第三个打印2,7,12... 就是 起始位置递增线程总数(例子以5个线程为例)如果需要连续,可以把数据分段, 如第一个线程打印0-200000 第二个打印200001-400000 ... 就是按1000000除以线程总数平分for example public class Test { public static void main(String[] args) { final int threadCount = 5; Thread[] t = new Thread[threadCount]; final int maxLoop = 1000000; for (int i=0; i<threadCount; i++) { final int j = i; t[i] = new Thread() { public void run() { for (int i=j; i<maxLoop; i+=threadCount) { System.out.println("value:" + i); } } }; t[i].start(); } } }
class TicketsSystem
{
public static void main(String[] args)
{
SellThread st=new SellThread();
new Thread(st).start();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
e.printStackTrace();
}
st.b=true;
new Thread(st).start();
//new Thread(st).start();
//new Thread(st).start();
}
}class SellThread implements Runnable
{
int tickets=100;
Object obj=new Object();
boolean b=false;
public void run()
{
if(b==false)
{
while(true)
sell();
}
else
{
while(true)
{
synchronized(obj)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
synchronized(this)
{
if(tickets>0)
{
System.out.println("obj:"+Thread.currentThread().getName()+
" sell tickets:"+tickets);
tickets--;
}
}
}
}
}
}
public synchronized void sell()
{
synchronized(obj)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("sell():"+Thread.currentThread().getName()+
" sell tickets:"+tickets);
tickets--;
}
}
}
}
static int k = 0;
final static int kmax = 1000000; synchronized static int getK() {
if (k < kmax) {
int bk = k;
k++;
return bk;
}
return -1;
} public static void main(String[] args) {
int threadMax = 10;
for (int j = 0; j < threadMax; j++) {
final Thread thr = new Thread("tname:" + j) { @Override
public void run() {
int i;
while ((i = getK()) != -1) {
System.out.println(Thread.currentThread() + "\tvalue:" + i);
try {
Thread.sleep(0);
} catch (InterruptedException ex) {
}
}
}
};
thr.setDaemon(true);
thr.start();
}
Scanner sc = new Scanner(System.in);
sc.nextLine();
}
//方法很多,我采取最简单的方法。还有1000000特别大,如果屏幕显示不完,打印到文件才能看出结果,或者把它的值变小
import java.util.concurrent.*;
public class Counter implements Runnable {
public static volatile int i = 0;
private static final int SIZE = 1000;//1000000;太 public static synchronized void next() {
if (i < SIZE)
System.out.println("value:" + i++);
} public void run() {
while (i < 10000)
next();
} public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
exec.execute(new Counter());
}
}
}
public static void main(String[] args) {
Print p = new Print();
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
Thread t3 = new Thread(p);
t1.start();
t2.start();
t3.start();
}
}class Print implements Runnable {
private int i;
@Override
public void run() {
synchronized (this) {
for (; i < 10000; i++) {
System.out.println("thread:" + Thread.currentThread().getName() + " value:" + i);
}
}
}}是这个意思吗,几个线程合作打印出结果。
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;public class TestMultiThread { public static void main(String[] args) throws InterruptedException, ExecutionException {
int amount = 10000;
int part = 1000;
int start = 0;
int end = 0;
ExecutorService threadPool = Executors.newCachedThreadPool();
Collection<Callable<String>> tasks = new ArrayList<Callable<String>>();
for(int i = 1; i <= amount/part; i++){
end = start + part;
tasks.add(new DisplayValue(start, end));
start = end;
}
List<Future<String>> results = threadPool.invokeAll(tasks);
threadPool.shutdown();
for(Future<String> result : results){
System.out.println(result.get());
}
}}class DisplayValue implements Callable<String>{ private int start = 0;
private int end = 0;
StringBuilder sb = new StringBuilder();
DisplayValue(int start, int end){
this.start = start;
this.end = end;
}
public String call() {
for(int i = start; i < end; i++){
// sb.append("value : ");
sb.append(i);
sb.append(" ");
if(i > start && i % 100 == 0){
sb.append("\n");
}
}
sb.append("\n");
return sb.toString();
}
}
如第一个线程打印0,5,10...
第二个打印1,6,11...
第三个打印2,7,12...
就是 起始位置递增线程总数(例子以5个线程为例)如果需要连续,可以把数据分段,
如第一个线程打印0-200000
第二个打印200001-400000
...
就是按1000000除以线程总数平分for example
public class Test {
public static void main(String[] args) {
final int threadCount = 5;
Thread[] t = new Thread[threadCount];
final int maxLoop = 1000000;
for (int i=0; i<threadCount; i++) {
final int j = i;
t[i] = new Thread() {
public void run() {
for (int i=j; i<maxLoop; i+=threadCount) {
System.out.println("value:" + i);
}
}
};
t[i].start();
}
}
}
{
public static void main(String [] args)
{
/*
for(int i=0;i<10000;i++)
{
System.out.println(i)
}
*/
SportFor s=new SportFor();
Thread t0=new Thread(s);
Thread t1=new Thread(s);
Thread t2=new Thread(s);
Thread t3=new Thread(s);
t0.start();
t1.start();
t2.start();
t3.start();
}
}class SportFor implements Runnable
{
int i=0;
public void run()
{ while(true)
{
synchronized(this)
{ if(i<=10000)
getNext();
else
return;
}
try
{
Thread.sleep(100);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
void getNext()
{
System.out.println(Thread.currentThread().getName()+" "+i++);
}
}
{
public static void main(String [] args)
{
/*
for(int i=0;i<10000;i++)
{
System.out.println(i)
}
*/
SportFor s=new SportFor();
Thread t0=new Thread(s);
Thread t1=new Thread(s);
Thread t2=new Thread(s);
Thread t3=new Thread(s);
t0.start();
t1.start();
t2.start();
t3.start();
}
}class SportFor implements Runnable
{
int i=0;
public void run()
{ while(true)
{
synchronized(this)
{ if(i<=10000)
getNext();
else
return;
}
try
{
Thread.sleep(100);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
void getNext()
{
System.out.println(Thread.currentThread().getName()+" "+i++);
}
}